我已经整理了一段代码,它在我的路线上执行 GET。我想使用模拟来测试这个。我是 Go 和测试菜鸟,因此非常感谢任何提示。
My Generate Routes.go 为当前 URL 生成路由。片段:
func (h *StateRoute) GenerateRoutes (router *martini.Router) *martini.Router {
r := *router
/**
* Get all states
*
*/
r.Get("/state", func( enc app.Encoder,
db abstract.MongoDB,
reqContext abstract.RequestContext,
res http.ResponseWriter,
req *http.Request) (int, string) {
states := []models.State{}
searchQuery := bson.M{}
var q *mgo.Query = db.GetDB().C("states").Find(searchQuery)
query, currentPage, limit, total := abstract.Paginate(req, q)
query.All(&states)
str, err := enc.EncodeWithPagination(currentPage, limit, total, states)
return http.StatusOK, app.WrapResponse(str, err)
})
}
这在我的 server.go 中被调用,如下所示:
var configuration = app.LoadConfiguration(os.Getenv("MYENV"))
// Our Martini API Instance
var apiInstance *martini.Martini
func init() {
apiInstance = martini.New()
// Setup middleware
apiInstance.Use(martini.Recovery())
apiInstance.Use(martini.Logger())
// Add the request context middleware to support contexual data availability
reqContext := &app.LRSContext{ }
reqContext.SetConfiguration(configuration)
producer := app.ConfigProducer(reqContext)
reqContext.SetProducer(producer)
apiInstance.MapTo(reqContext, (*abstract.RequestContext)(nil))
// Hook in the OAuth2 Authorization object, to be processed before all requests
apiInstance.Use(app.VerifyAuthorization)
// Connect to the DB and Inject the DB connection into Martini
apiInstance.Use(app.MongoDBConnect(reqContext))
// Add the ResponseEncoder to allow JSON encoding of our responses
apiInstance.Use(app.ResponseEncoder)
}
我的疑惑:
考虑到我正在尝试注入依赖项,这里的模拟如何工作?
我应该从哪里开始测试,即我应该在 Generate Routes 中模拟 r.Get 吗?现在,我已经这样做了,但是由于我使用的是 Martini 来处理所有路由和请求,如果我所做的是正确的,我会丢失报价吗?
相关分类