所以這個真的很奇怪,我試圖得到一個呈現 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()。
- 1 回答
- 0 關注
- 269 瀏覽
添加回答
舉報
0/150
提交
取消