Unit testing 使用aetest.NewContext代替endpoints.NewContext测试具有强一致性的go端点
我有一个搜索方法:Unit testing 使用aetest.NewContext代替endpoints.NewContext测试具有强一致性的go端点,unit-testing,google-app-engine,go,google-cloud-endpoints,Unit Testing,Google App Engine,Go,Google Cloud Endpoints,我有一个搜索方法: func (sa *SearchApi) Search(c endpoints.Context, r *SearchQuery) (*SearchResults, error) { .. } 如您所见,它需要一个端点。上下文例如: ctx := endpoints.NewContext(req1) 但是,对于aetest,我使用了不同的上下文: otherCtx, err := aetest.NewContext(&aetest.Options{"", true}
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传递给我的搜索方法,因为它不是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端点的推荐方法是什么?是否可以将aetest上下文转换为端点上下文?如何:
inst := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: true})
r, _ := inst.NewRequest("GET", "/", nil)
c := endpoints.NewContext(r)
sa.Search(c, ...)
根据alex所说,我有以下解决方案:
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)
}