Gin: How to unit test properly

Created on 9 Dec 2017  路  1Comment  路  Source: gin-gonic/gin

Hello All,

I'm seeking the best way to unit testing my handler:

In main file:
` func init() {
db.Connect()
}

func main() {
// Init API
api := gin.Default()

// Using Middle-wares get the db session in the context for each request
api.Use(middlewares.Connect)

    routerProjects := api.Group("/projects")
{
    // All Projects
    routerProjects.GET("", projects.ListAllProjects)
    }

`

Here is my handler function
`
func ListAllProjects(c *gin.Context) {

session := c.MustGet("session").(*mgo.Session)
db := session.DB("myDB")

var projects []models.ProjectList
err := db.C("projects").Find(bson.M{}).All(&projects)

if err != nil {
    helpers.RespondWithError(400, err.Error(), c)
}

c.JSON(200, projects)

}
`

Please advise. Thank you

Most helpful comment

>All comments

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CodingPapi picture CodingPapi  路  3Comments

lilee picture lilee  路  3Comments

nxvl picture nxvl  路  3Comments

wangcn picture wangcn  路  3Comments

iiinsomnia picture iiinsomnia  路  3Comments