所以这个真的很奇怪,我试图得到一个呈现 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)
}
}
我正在测试该方法messageFromResponse,MyController但它甚至没有构建。当我go test在我的项目目录中运行时,我收到以下错误:
./my_controller_test.go:115: w.Result undefined (type *httptest.ResponseRecorder has no field or method Result)
我还应该提到,我httptest.ResponseRecorder在同一个文件的其他地方成功地用作写入存根,但是当我尝试访问时它只是失败了Result()。
holdtom
相关分类