Skip to content

Instantly share code, notes, and snippets.

@joshuabezaleel
Created January 8, 2020 03:12
Show Gist options
  • Save joshuabezaleel/6ab7873f708074eccef20bbf1a9bdc6d to your computer and use it in GitHub Desktop.
Save joshuabezaleel/6ab7873f708074eccef20bbf1a9bdc6d to your computer and use it in GitHub Desktop.
func TestCreate(t *testing.T) {
initialBook := book.NewBook("testID", "testTitle", "testAuthor")
bookService.On("Create", initialBook).Return(initialBook, nil)
url := fmt.Sprintf("/books")
jsonReq, _ := json.Marshal(initialBook)
req := httptest.NewRequest("POST", url, bytes.NewBuffer(jsonReq))
rr := httptest.NewRecorder()
bookTestingHandler.getBook(rr, req)
resp := rr.Result()
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
require.Nil(t, err)
createdBook := book.Book{}
err = json.Unmarshal(body, &createdBook)
require.Nil(t, err)
require.Equal(t, http.StatusCreated, resp.StatusCode)
require.Equal(t, initialBook.ID, createdBook.ID)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment