2 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊
我找到了一種使用ginkgo 的方法。
測(cè)試GET請(qǐng)求:
Describe("GET /login", func() {
It("response has http code 200", func() {
request, _ := http.NewRequest("GET", "/login", nil)
response := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(response, request)
Expect(response.Code).To(Equal(http.StatusOK))
})
})
測(cè)試POST請(qǐng)求:
Describe("POST /login", func() {
Context("when passwords don't match", func() {
It("informs about error", func() {
form := url.Values{
"password": {"foobar"},
"password-confirmation": {"barfoo"},
}
body := strings.NewReader(form.Encode())
request, _ := http.NewRequest("POST", "/login", body)
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
response := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(response, request)
Expect(response.Code).To(Equal(http.StatusOK))
Expect(response.Body.String()).To(ContainSubstring("wrong passwords..."))
})
})
})
同樣在BeforeSuite我需要初始化路由器和調(diào)用beego.TestBeegoInit(<APP_PATH>)
var _ = BeforeSuite(func() {
routers.Initialize() // here calling router code
beego.TestBeegoInit(AppPath())
})

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊
這是一個(gè) beego 示例項(xiàng)目。在 test 文件夾中,它展示了如何為控制器編寫單元測(cè)試 https://github.com/goinggo/beego-mgo
- 2 回答
- 0 關(guān)注
- 354 瀏覽
添加回答
舉報(bào)