简体   繁体   English

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

[英]How to write test cases for beego app?

How to approach writing test cases for Beego app. 如何为Beego app编写测试用例。 As i can see on Beego website, they have model test case but what about controllers? 正如我在Beego网站上看到的那样,他们有模型测试案例,但控制器呢?

Any Framework which can help? 任何可以帮助的框架?

I've found one approach using ginkgo . 我找到了一种使用银杏的方法。

Testing a GET request: 测试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))
    })
})

Testing a POST request: 测试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..."))
        })
    })
})

Also in the BeforeSuite I needed to initialize the routers and calling beego.TestBeegoInit(<APP_PATH>) 同样在BeforeSuite我需要初始化路由器并调用beego.TestBeegoInit(<APP_PATH>)

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

This is a beego sample project. 这是一个beego示例项目。 In the test folder, it shows how to write unit test for controllers https://github.com/goinggo/beego-mgo 在测试文件夹中,它显示了如何为控制器编写单元测试https://github.com/goinggo/beego-mgo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM