我有一个搜索方法:
func (sa *SearchApi) Search(c endpoints.Context, r *SearchQuery) (*SearchResults, error) { .. }
如您所见,它需要一个端点。上下文例如:
ctx := endpoints.NewContext(req1)
但是对于 aetest,我使用的是不同的上下文:
otherCtx, err := aetest.NewContext(&aetest.Options{"", true})
特别是这个上下文有额外的强一致性选项 - 因为我正在设置数据所以我可以测试一个只读的 api。
我无法将 otherCxt 传递给我的 Search 方法,因为它不是 endpoints.Context
其他Ctx:
type Context interface {
appengine.Context
// Login causes the context to act as the given user.
Login(*user.User)
// Logout causes the context to act as a logged-out user.
Logout()
// Close kills the child api_server.py process,
// releasing its resources.
io.Closer
}
端点。上下文:
type Context interface {
appengine.Context
// HTTPRequest returns the request associated with this context.
HTTPRequest() *http.Request
// Namespace returns a replacement context that operates within the given namespace.
Namespace(name string) (Context, error)
// CurrentOAuthClientID returns a clientID associated with the scope.
CurrentOAuthClientID(scope string) (string, error)
// CurrentOAuthUser returns a user of this request for the given scope.
// It caches OAuth info at the first call for future invocations.
//
// Returns an error if data for this scope is not available.
CurrentOAuthUser(scope string) (*user.User, error)
}
使用 aetest 测试 go-endpoints 的推荐方法是什么?是否可以将 aetest 上下文转换为端点上下文?
最佳答案
这个怎么样:
inst := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true})
r, _ := inst.NewRequest("GET", "/", nil)
c := endpoints.NewContext(r)
sa.Search(c, ...)
关于unit-testing - 使用 aetest.NewContext 代替 endpoints.NewContext 以强一致性测试 go-endpoints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28911212/