2 回答
TA贡献1790条经验 获得超9个赞
如果您不反对使用Martini,则依赖注入是解决问题的好方法。以下是设置测试的方法(使用Ginkgo):
var _ = Describe("Items", func() {
var (
m *martini.ClassicMartini
)
BeforeEach(func() {
m = martini.Classic()
// injects app engine context into requests
m.Use(func(c martini.Context, req *http.Request) {
con, _ := aetest.NewContext(nil)
c.MapTo(con, (*appengine.Context)(nil))
})
m.Get("/items", func(c martini.Context){
// code you want to test
})
})
It("should get items", func() {
recorder := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/items", nil)
m.ServeHTTP(recorder, r) // martini server used
Expect(recorder.Code).To(Equal(200))
})
})
在生产中,实际的上下文将被注入:
m.Use(func(c martini.Context, req *http.Request) {
c.MapTo(appengine.NewContext(req), (*appengine.Context)(nil))
})
- 2 回答
- 0 关注
- 203 浏览
添加回答
举报