gpt4 book ai didi

unit-testing - httptest.ResponseRecorder 没有字段或方法结果

转载 作者:IT王子 更新时间:2023-10-29 01:09:42 24 4
gpt4 key购买 nike

所以这个真的很奇怪,我正在尝试获得呈现 JSON 的模拟响应。我的测试看起来像这样:

import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
...

func TestMessageFromResponse(t *testing.T) {
mc := MyController{}
body := "{ \"message\":\"kthxbai\" }"
w := httptest.NewRecorder()
w.Write([]byte(body))
resp := w.Result()
msg := mc.messageFromResponse(resp)
if msg != "kthxbai" {
t.Errorf("Expected response body to be kthxbai but was %s", msg)
}
}

我正在 MyController 上测试方法 messageFromResponse 但它甚至没有构建。当我在项目目录中运行 go test 时,出现以下错误:

./my_controller_test.go:115: w.Result undefined (type *httptest.ResponseRecorder has no field or method Result)

我还应该提到,我在同一文件的其他地方成功地使用了 httptest.ResponseRecorder 作为编写器 stub ,但是当我尝试访问 Result() 时它只是失败了>.

最佳答案

我最初将其作为评论来处理,因为我不确定相关性,但为了后代:

在线godoc始终引用最新版本。在这种情况下,Result()方法是在上周才发布的Go1.7中添加的。如有疑问,请运行 godoc -server 检查您的本地 godoc。或 godoc <packagename> .

关于unit-testing - httptest.ResponseRecorder 没有字段或方法结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39112511/

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