gpt4 book ai didi

Golang 获取带有 httptest 失败的错误消息

转载 作者:IT王子 更新时间:2023-10-29 02:17:39 25 4
gpt4 key购买 nike

当您返回 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, but where is the error message?
}

最佳答案

查看 httptest.Recorder ( http://golang.org/pkg/net/http/httptest/#example_ResponseRecorder) 的示例,它看起来 res.Body.String() 就是您想要的。

关于Golang 获取带有 httptest 失败的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21404553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com