How to write test cases for beego app?
Asked Answered
B

2

7

How to approach writing test cases for Beego app. As i can see on Beego website, they have model test case but what about controllers?

Any Framework which can help?

Bluma answered 12/2, 2016 at 7:45 Comment(0)
E
2

I've found one approach using ginkgo.

Testing a GET request:

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:

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>)

var _ = BeforeSuite(func() {
    routers.Initialize() // here calling router code
    beego.TestBeegoInit(AppPath())
})
Excavation answered 31/1, 2019 at 15:21 Comment(0)
N
1

This is a beego sample project. In the test folder, it shows how to write unit test for controllers https://github.com/goinggo/beego-mgo

Nirvana answered 21/10, 2016 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.