英文:
Golang getting error messages with httptest failures
问题
当你收到一个500服务器错误时,你可以通过httptest获取错误消息吗?
手动测试注册页面是正常工作的,所以这似乎是一些管道问题,但我找不到错误消息在哪里。
func TestSignUp(t *testing.T) {
var (
password = "password"
)
newUser := entities.User{}
newUser.Email = "j@doe.co.za"
newUser.SetPassword(password, bcrypt.MinCost)
v := url.Values{}
v.Add("email_address", newUser.Email)
v.Add("password", password)
res := httptest.NewRecorder()
req := &http.Request{
Method: "POST",
URL: &url.URL{Path: "/signup"},
Form: v,
}
m := Martini()
m.ServeHTTP(res, req)
assert.Equal(t, 200, res.Code) <<< res.Code = 500, 但错误消息在哪里?
}
请注意,这只是代码的翻译部分,不包括任何其他内容。
英文:
How can one get the error message with httptest when you get back a 500 server error?
The sign up page works when manually tested, so this appears to be some plumbing problem, but I cannot find what the message is.
func TestSignUp(t *testing.T) {
var (
password = "password"
)
newUser := entities.User{}
newUser.Email = "j@doe.co.za"
newUser.SetPassword(password, bcrypt.MinCost)
v := url.Values{}
v.Add("email_address", newUser.Email)
v.Add("password", password)
res := httptest.NewRecorder()
req := &http.Request{
Method: "POST",
URL: &url.URL{Path: "/signup"},
Form: v,
}
m := Martini()
m.ServeHTTP(res, req)
assert.Equal(t, 200, res.Code) <<<< res.Code = 500, but where is the error message?
}
答案1
得分: 1
根据httptest.Recorder的示例(http://golang.org/pkg/net/http/httptest/#example_ResponseRecorder),看起来你需要使用res.Body.String()
。
英文:
Looking at the example for httptest.Recorder (http://golang.org/pkg/net/http/httptest/#example_ResponseRecorder) it looks res.Body.String()
is what you want.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论