dongposhi8677 2019-06-10 20:53
浏览 85

测试用例在gin框架中不起作用

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 关于#python#的问题:自动化测试