#Go Test

3 messages · Page 1 of 1 (latest)

lone arrow
#

I'm trying to write a unit test for create-user endpoint, which is an authorized one.
I tried case of 401 first. My test got passed, but when it comes to coverage it returns 0.0% of statements.
My function takes db and gin.Context as parameters .
func CreateUserProfile(db *gorm.DB, cache utils.CacheItf, c *gin.Context) {
user := models.User{}
response := models.UserResponse{}
userProfile := Profile{}

ID ,_:= c.Get("ID")
if err := db.Find(&user, ID).Error; err != nil {
    response.Data = nil
    response.Status = false
    response.StatusMessage = "User Not found" + err.Error()
    response.TimeStamp = time.Now().UTC()
    utils.RespondJSON(c, http.StatusNotFound, response)
    return
}
decoder := json.NewDecoder(c.Request.Body)
if err := decoder.Decode(&userProfile); err != nil {
    utils.RespondError(c, http.StatusBadRequest, err.Error())
    return
}
if err := db.Model(&user).Updates(models.User{FirstName: userProfile.FirstName, LastName: userProfile.LastName, Email: userProfile.Email}).Error; err != nil {
    response.Data = nil
    response.Status = false
    response.StatusMessage = "Failed to create User Profile!" + err.Error()
    response.TimeStamp = time.Now().UTC()
    utils.RespondJSON(c, http.StatusBadRequest, response)
    return
} else if err == nil {
    user.FirstName = userProfile.FirstName
    user.LastName = userProfile.LastName
    user.Email = userProfile.Email

    response.Data = user
    response.Status = true
    response.StatusMessage = "Profile created successfully!"
    response.TimeStamp = time.Now().UTC()
    utils.RespondJSON(c, http.StatusCreated, response)
}

}
Can you help me with unit test for this?🤔

river pumice
#

Can you send the tests too?

#

Also, are your tests files written in the same folder as your code?