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
@jayhuang75 please see https://github.com/gin-gonic/gin#testing
Most helpful comment
@jayhuang75 please see https://github.com/gin-gonic/gin#testing