如何为beego应用编写测试用例?

7
如何编写 Beego 应用程序的测试用例?在 Beego 网站上,我们可以看到它们有模型测试用例,但控制器呢?
是否有任何框架可以帮助?
2个回答

2
我发现一种使用ginkgo的方法。
测试一个GET请求:
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))
    })
})

测试一个POST请求:

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中,我需要初始化路由器并调用beego.TestBeegoInit(<APP_PATH>)

var _ = BeforeSuite(func() {
    routers.Initialize() // here calling router code
    beego.TestBeegoInit(AppPath())
})

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接