Reputation: 3329
So this one is really weird, I'm trying to get a mock response that renders JSON. My test looks like 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 to be kthxbai but was %s", msg)
}
}
I'm testing the method messageFromResponse
on MyController
but its not even building. When I run go test
in my project directory, I get the following error:
./my_controller_test.go:115: w.Result undefined (type *httptest.ResponseRecorder has no field or method Result)
I should also mention that I am successfully using httptest.ResponseRecorder
as a writer stub elsewhere in this same file, but it's just failing when I try to access Result()
.
Upvotes: 3
Views: 867
Reputation: 828
I initially addressed this as a comment because I was unsure of relevance, but for posterity:
The online godoc always references the latest release. In this case, the Result()
method was added in Go1.7 which only released last week. When in doubt, check your local godoc by running godoc -server
or godoc <packagename>
.
Upvotes: 2