Search code examples
gotestngmicroservicesgo-testing

Test fails to capture logging output


I am trying to test my UserRegister functionality, it takes http request.

If user enters already existing email, UserRegister returns an error log (using logrus).

logs "github.com/sirupsen/logrus"

func UserRegister(res http.ResponseWriter, req *http.Request) {

    requestID := req.FormValue("uid")
    email := req.FormValue("email")

    logs.WithFields(logs.Fields{
        "Service":  "User Service",
        "package":  "register",
        "function": "UserRegister",
        "uuid":     requestID,
        "email":    email,
    }).Info("Received data to insert to users table")

    // check user entered new email address
    hasAccount := checkemail.Checkmail(email, requestID) // returns true/false

    if hasAccount != true { // User doesn't have an account

        db := dbConn()

        // Inserting token to login_token table
        insertUser, err := db.Prepare("INSERT INTO users (email) VALUES(?)")
        if err != nil {
            logs.WithFields(logs.Fields{
                "Service":  "User Service",
                "package":  "register",
                "function": "UserRegister",
                "uuid":     requestID,
                "Error":    err,
            }).Error("Couldnt prepare insert statement for users table")
        }
        insertUser.Exec(email)
        defer db.Close()
        return
    } // user account created

    logs.WithFields(logs.Fields{
        "Service":  "User Service",
        "package":  "register",
        "function": "UserRegister",
        "uuid":     requestID,
        "email":    email,
    }).Error("User has an account for this email")


}

In my test module, I used following.

func TestUserRegister(t *testing.T) {
    rec := httptest.NewRecorder()
    req, _ := http.NewRequest("POST", "http://localhost:7071/[email protected]&uid=sjfkjsdkf9w89w83490w", nil)

    UserRegister(rec, req)

    expected := "User has an account for this email"

    res := rec.Result()
    content, err := ioutil.ReadAll(res.Body)

    if err != nil {
        t.Error("Couldnt read body", err)
    }

    val, err := strconv.Atoi(string(bytes.TrimSpace(content)))

    if err != nil {
        log.Println("Error parsing response", err)
    }
    if string(val) != expected { 
        t.Errorf("Expected %s, got %s", expected, string(content))
    }

}

Result : Error parsing response strconv.Atoi: parsing "": invalid syntax

Why response can not be converted?

Checked threads:

Why is this Golang code to convert a string to an integer failing.

Edit : after @chmike answer.

This is a part of microservice. All the responses are written to API-Gateway. Using a function.

But here I just want to perform unit test and check whether my UserRegister works as expected.


Solution

  • The function UserRegister never writes to res or sets the status. As a consequence you get an empty string from res in TestUserRegister. content is an empty string and the conversion of an empty string to an integer with Atoi fails since there is no integer to convert.

    I can only explain what happens. I can’t tell you what to do to fix the problem because you don’t explain what you want to do or get in the question.