I have written test case to test method in Gin-Gonic framework but it in not giving expected output while running test case. But when I run method using API in Postman it is working properly.
Test case
func TestList(t *testing.T) {
router := gin.New()
router.GET("/testList", things.List)
req, _ := http.NewRequest("GET", "/stuff", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.NotNil(t, resp.Body.String())
assert.Equal(t, 201,resp.Code, resp.Body.String())
}
Method on which test case created
func List(c *gin.Context) {
db := c.MustGet("db").(*mgo.Database)
things := []models.Thing{}
err := db.C(models.CollectionStuff).Find(nil).All(&things)
if err != nil {
c.Error(err)
}
c.JSON(http.StatusOK, things)
}
Router
func main() {
router := gin.Default()
router.Use(middlewares.Connect)
router.GET(Prefix +"/things", things.List)
}
When I run test case using go test -v ./... Output
Error Trace: thing_test.go:31
Error: Not equal:
expected: 201
actual : 404
Test: TestList
Messages: 404 page not found
While running through API it works but not while running through test case.