Skip to content

Instantly share code, notes, and snippets.

@krishbhanushali
Created April 11, 2019 04:58
Show Gist options
  • Save krishbhanushali/fe667c86e79a478eea7a7dc5ed3782d0 to your computer and use it in GitHub Desktop.
Save krishbhanushali/fe667c86e79a478eea7a7dc5ed3782d0 to your computer and use it in GitHub Desktop.
This Gist gives you the rough idea about how to
func TestGetEntries(t *testing.T) {
req, err := http.NewRequest("GET", "/entries", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(GetEntries)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
// Check the response body is what we expect.
expected := `[{"id":1,"first_name":"Krish","last_name":"Bhanushali","email_address":"krishsb@g.com","phone_number":"0987654321"},{"id":2,"first_name":"xyz","last_name":"pqr","email_address":"xyz@pqr.com","phone_number":"1234567890"},{"id":6,"first_name":"FirstNameSample","last_name":"LastNameSample","email_address":"lr@gmail.com","phone_number":"1111111111"}]`
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment