any suggestions on how to write some unit tests for the controllers?
now I have no idea on the unittest. My project is do like this, all the logic i put in the model, model is base on func. so i can writer unittest base on the model. While the controller's func is hard to writer unittest
thanks, that's exactly how I built my app. the controllers contain almost no logic, but it would still be nice to have some sort of way to unit test them.
yeah, I was thinking about how to write a small test framework base on beego.May be when beego 1.0 release. If you have any good idea, welcome tell me
可以mock数据数据库,建立测试数据库.然后使用下面的代码做数据库mock
db.MustSetTablesDataYaml(`
user:
- id: 10
name: A
- id: 11
name: B
friend_ref:
- id: 1
from_id: 10
to_id: 11
`)
This can be done by using httptest.NewRecorder as beego already did in router_test.go. But you need to explicitly copy router from your
main.go to test module, as the following:
func TestPrepare(t *testing.T) {
r, _ := http.NewRequest("GET", "/json/list", nil)
w := httptest.NewRecorder()
handler := NewControllerRegistor()
handler.Add("/json/list", &JsonController{})
handler.ServeHTTP(w, r)
if w.Body.String() != `"prepare"` {
t.Errorf(w.Body.String() + "user define func can't run")
}
}
this is an beego sample. In the test folder show how to unit test
https://github.com/goinggo/beego-mgo
Most helpful comment
this is an beego sample. In the test folder show how to unit test
https://github.com/goinggo/beego-mgo