unit-testing - 使用 aetest.NewContext 代替 endpoints.NewContext 以强一致性测试 go-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})

特别是这个上下文有额外的强一致性选项 - 因为我正在设置数据所以我可以测试一个只读的 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/

相关文章:

戈朗 : fatal error: unexpected signal during runtime execution

c# - 读取文件的集成测试

java - 使用 Mockito 测试抽象类

python - 来自 ndb.DateTimeProperty(auto_true=now) 的 ndb.CompulatedProperty 第一次调用错误

python - 如何更改 NDB 记录的祖先?

templates - Beego如何停止对模板文件中的url进行编码?

c# - 如何将单元测试分成组

unit-testing - 使用 express 在 node.js 中测试 HTTP 请求

google-app-engine - 检查数据存储中是否已存在值的最佳方法

go - Go语言中初始化和归零,new()和make()的区别