Skip to content

Instantly share code, notes, and snippets.

@diegoholiveira
Created April 28, 2020 22:02
Show Gist options
  • Save diegoholiveira/d4758b609be4d3dcf03e8518b070cccc to your computer and use it in GitHub Desktop.
Save diegoholiveira/d4758b609be4d3dcf03e8518b070cccc to your computer and use it in GitHub Desktop.
func TestPurchase_success(t *testing.T) {
input := `{
"user_id": 1,
"books": [
{
"id": 1,
"quantity": 2,
"price": 19.9
}
]
}`
purchase := purchases.Purchase{
UserID: 1,
Books: purchases.Books{
{
ID: 1,
Quantity: 2,
Price: 19.9,
},
},
}
r := httptest.NewRequest("POST", "/purchases", strings.NewReader(input))
w := httptest.NewRecorder()
persister := new(mocks.PurchasePersister)
persister.
On("Persist", mock.Anything, purchase).
Return(nil)
h := NewPurchaseHandler(persister)
h.ServeHTTP(w, r)
resp := w.Result()
defer resp.Body.Close()
if assert.Equal(t, http.StatusCreated, resp.StatusCode) {
persister.AssertExpectations(t)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment