我有一个搜索方法:func (sa *SearchApi) Search(c endpoints.Context, r *SearchQuery) (*SearchResults, error) { .. }如您所见,它需要一个 endpoints.Context 例如: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 上下文转换为端点上下文?
2 回答
呼如林
TA贡献1798条经验 获得超3个赞
根据亚历克斯所说,我有以下解决方案:
func TestApi(t *testing.T) {
inst, _ := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true})
defer inst.Close()
r, _ := inst.NewRequest("GET", "/", nil)
c := endpoints.NewContext(r)
makeCourses(c)
api := SearchApi{}
searchQuery := &SearchQuery{"my-search-term", nil}
searchResults, _ := api.Search(c, searchQuery)
log.Println("got results %v", searchResults)
}
胡说叔叔
TA贡献1804条经验 获得超8个赞
这个怎么样:
inst := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true})
r, _ := inst.NewRequest("GET", "/", nil)
c := endpoints.NewContext(r)
sa.Search(c, ...)
- 2 回答
- 0 关注
- 198 浏览
添加回答
举报
0/150
提交
取消