unit testing - httptest.ResponseRecorder has no field or method Result -
so 1 weird, i'm trying mock response renders json. test looks this:
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 kthxbai %s", msg) } } i'm testing method messagefromresponse on mycontrollerbut not building. when run go test in project directory, following error:
./my_controller_test.go:115: w.result undefined (type *httptest.responserecorder has no field or method result) i should mention using httptest.responserecorder writer stub elsewhere in same file, it's failing when try access result().
i addressed comment because unsure of relevance, posterity:
the online godoc references latest release. in case, result() method added in go1.7 released last week. when in doubt, check local godoc running godoc -server or godoc <packagename>.
Comments
Post a Comment