Skip to content

Commit

Permalink
example unit tests for all endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
fogfish committed Mar 19, 2023
1 parent 6d15f63 commit 4b0e0e0
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions http/petshop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,58 @@ func TestPetShopListFailed(t *testing.T) {
),
)
}

func TestPetShopLookup(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mpf := mock.NewMockPetFetcher(ctrl)
mpf.EXPECT().LookupPet(
gomock.Any(),
gomock.Eq("A01"),
).Return(mock.Pets[0], nil)

service := http.NewPetShopAPI(mpf, nil)
httpd := µmock.Endpoint(service.Lookup())
yield := httpd(µmock.Input(
µmock.Method("GET"),
µmock.URL("/petshop/pets/A01"),
µmock.Header("Accept", "application/json"),
))

it.Then(t).Should(
it.Equiv(yield,
ø.Status.OK(
ø.ContentType.ApplicationJSON,
ø.Send(api.NewPet(mock.Pets[0])),
),
),
)
}

func TestPetShopCreate(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mpc := mock.NewMockPetCreator(ctrl)
mpc.EXPECT().CreatePet(
gomock.Any(),
gomock.Eq(mock.Pets[0]),
).Return(nil)

service := http.NewPetShopAPI(nil, mpc)
httpd := µmock.Endpoint(service.Create())
yield := httpd(µmock.Input(
µmock.Method("POST"),
µmock.URL("/petshop/pets"),
µmock.Header("Accept", "application/json"),
µmock.Header("Authorization", "Basic cGV0c3RvcmU6b3duZXIK"),
µmock.JSON(mock.Pets[0]),
))

it.Then(t).Should(
it.Equiv(yield,
ø.Status.Created(),
),
)
}

0 comments on commit 4b0e0e0

Please sign in to comment.