Reputation: 823
I have the following function:
func SomeFunction(w http.ResponseWriter, ...) error{
.
.
.
return json.NewEncoder(w).Encode(&c)
}
where c is my struct. When I run the function I receive the response body with a json but I can't test this function. For example, I have:
func TestSomeFunction(t *testing.T) {
.
.
.
w := httptest.ResponseRecorder{}
err := SomeFunction(w, ...)
.
.
}
My w.Body is empty in this case. The problem isn't my function because I when I run I get the body. I think I'm not testing in the right way. How is the right way to test it?
Upvotes: 0
Views: 5325
Reputation: 1174
You should be using httptest.NewRecorder()
to get your recorder.... additionally I wouldn't test against the recorder's Bytes
.... check the Result
https://golang.org/pkg/net/http/httptest/#ResponseRecorder.Result
Upvotes: 2