Golang在处理httptest失败时出现错误消息。

huangapple go评论85阅读模式
英文:

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 = &quot;password&quot;
	)

	newUser := entities.User{}
	newUser.Email = &quot;j@doe.co.za&quot;	
	newUser.SetPassword(password, bcrypt.MinCost)

	v := url.Values{}
	v.Add(&quot;email_address&quot;, newUser.Email)
	v.Add(&quot;password&quot;, password)    	

	res := httptest.NewRecorder()
	req := &amp;http.Request{
		Method: &quot;POST&quot;,
		URL:    &amp;url.URL{Path: &quot;/signup&quot;},
		Form:   v,
	}

	m := Martini()
	m.ServeHTTP(res, req)

	assert.Equal(t, 200, res.Code) &lt;&lt;&lt;&lt; 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.

huangapple
  • 本文由 发表于 2014年1月28日 19:35:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/21404553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定