英文:
Does aetest.NewContext() require arguments?
问题
我正在学习如何为一个Appengine应用创建Golang测试。
文档中的示例对我来说不太明确。
https://cloud.google.com/appengine/docs/standard/go/tools/localunittesting/reference
文档似乎说可以创建一个context := aetest.NewContext()
但是当我尝试这样做时,我得到一个错误,提示aetest.NewContext需要参数。
$ go test -v
./skincare_test.go:12: 在调用aetest.NewContext时参数不足
有 ()
想要 (*aetest.Options)
./skincare_test.go:12: 赋值计数不匹配: 3 = 2
FAIL _/Users/Bryan/work/gocode/skincarereview [构建失败]
skincare_test.go的内容如下:
package skincare
import (
"net/http"
"net/http/httptest"
"testing"
"appengine/aetest"
)
func TestIndexHandler(t *testing.T) {
ctx, done, err := aetest.NewContext()
if err != nil {
t.Fatal(err)
}
defer done()
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(root)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler返回了错误的状态码: 得到 %v 想要 %v",
status, http.StatusOK)
}
expected := "<div>Name"
if rr.Body.String() != expected {
t.Errorf("handler返回了预期的body: 得到 %v 想要 %v",
rr.Body.String(), expected)
}
}
我通过查看示例代码来学习,我在哪里可以找到使用Appengine datastore的Go Web应用程序的测试示例?
文档中的示例太简单了,我看不出如何进行更复杂的测试。
英文:
I am learning how to create Golang tests for an Appengine app.
The documentation examples don't make sense to me.
https://cloud.google.com/appengine/docs/standard/go/tools/localunittesting/reference
Documentation seems to say you can create a context := aetest.NewContext()
When I attempt to do so, I'm getting an error that aetest.NewContext requires arguments.
$ go test -v
./skincare_test.go:12: not enough arguments in call to aetest.NewContext
have ()
want (*aetest.Options)
./skincare_test.go:12: assignment count mismatch: 3 = 2
FAIL _/Users/Bryan/work/gocode/skincarereview [build failed]
content of skincare_test.go:
package skincare
import (
"net/http"
"net/http/httptest"
"testing"
"appengine/aetest"
)
func TestIndexHandler(t *testing.T) {
ctx, done, err := aetest.NewContext()
if err != nil {
t.Fatal(err)
}
defer done()
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(root)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
expected := "<div>Name"
if rr.Body.String() != expected {
t.Errorf("handler returned expected body: got %v want %v",
rr.Body.String(), expected)
}
}
I learn best by looking at example code, where can I find examples of Tests for Go web applications that use Appengine datastore?
The examples in the documentation are so simple that I don't see how I'm supposed to do more complicated testing.
答案1
得分: 1
它说了两件事:
1)你缺少一个必需的参数*aetest.Options
。
2)你不能将由两个变量组成的aetest.NewContext()
的结果分配给三个变量的集合。
检查函数的输出是什么。我猜它只是(context.Context, error)
- 我怀疑done
以某种方式移动到了*aetest.Options
中。
不幸的是,我现在无法访问文档。
英文:
It says 2 things:
-
You are missing a required parameter
*aetest.Options
-
that you can NOT assign result
aetest.NewContext()
that consist of 2 variable to a set of 3 variables.
Check what is the output of the function. I guess it is just (context.Context, error)
- I suspect the done
is moved to the *aetest.Options somehow.
Unfortunately my access to docs is blocked right now.
答案2
得分: 0
你正在使用旧版本的应用引擎包(appengine/aetest
而不是google.golang.org/appengine/aetest
)。新版本不需要参数。
英文:
You are using the old version of the app engine package (appengine/aetest
instead of google.golang.org/appengine/aetest
). The newer version does not require arguments.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论