Skip to content

Commit

Permalink
Expose unmatched mocks in test result (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
steinfletcher authored Jul 14, 2020
1 parent 59e14bc commit dadd9b0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
36 changes: 29 additions & 7 deletions apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ func (a *APITest) Mocks(mocks ...*Mock) *APITest {
for i := range mocks {
times := mocks[i].response.mock.times
for j := 1; j <= times; j++ {
m = append(m, mocks[i].copy())
mockCpy := mocks[i].copy()
mockCpy.times = 1
m = append(m, mockCpy)
}
}
a.mocks = m
Expand Down Expand Up @@ -513,18 +515,38 @@ func (r *Response) End() Result {
}

apiTest.started = time.Now()
var res *http.Response
if apiTest.reporter != nil {
res := apiTest.report()
return Result{Response: res}
res = apiTest.report()
} else {
res = r.runTest()
}
res := r.runTest()

return Result{Response: res}
var unmatchedMocks []UnmatchedMock
for _, m := range r.apiTest.mocks {
if m.isUsed == false {
unmatchedMocks = append(unmatchedMocks, UnmatchedMock{
URL: *m.request.url,
})
break
}
}

return Result{
Response: res,
unmatchedMocks: unmatchedMocks,
}
}

// Result provides the final result
type Result struct {
Response *http.Response
Response *http.Response
unmatchedMocks []UnmatchedMock
}

// UnmatchedMocks returns any mocks that were not used, e.g. there was not a matching http Request for the mock
func (r Result) UnmatchedMocks() []UnmatchedMock {
return r.unmatchedMocks
}

// JSON unmarshal the result response body to a valid struct
Expand Down Expand Up @@ -700,7 +722,7 @@ func (r *Response) runTest() *http.Response {
func (a *APITest) assertMocks() {
for _, mock := range a.mocks {
if mock.isUsed == false && mock.timesSet {
a.verifier.Fail(a.t, fmt.Sprintf("mock was not invoked expected times: '%d'", mock.times))
a.verifier.Fail(a.t, "mock was not invoked expected times")
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions apitest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,11 +917,11 @@ func TestApiTest_ErrorIfMockInvocationsDoNotMatchTimes(t *testing.T) {

verifier := mocks.NewVerifier()
verifier.FailFn = func(t *testing.T, failureMessage string, msgAndArgs ...interface{}) bool {
assert.Equal(t, "mock was not invoked expected times: '2'", failureMessage)
assert.Equal(t, "mock was not invoked expected times", failureMessage)
return true
}

apitest.New().
res := apitest.New().
Mocks(getUser).
Verifier(verifier).
Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -932,6 +932,10 @@ func TestApiTest_ErrorIfMockInvocationsDoNotMatchTimes(t *testing.T) {
Expect(t).
Status(http.StatusOK).
End()

unmatchedMocks := res.UnmatchedMocks()
assert.NotEmpty(t, unmatchedMocks)
assert.Equal(t, "http://localhost:8080", unmatchedMocks[0].URL.String())
}

func TestApiTest_MatchesTimes(t *testing.T) {
Expand All @@ -942,7 +946,7 @@ func TestApiTest_MatchesTimes(t *testing.T) {
Times(1).
End()

apitest.New().
res := apitest.New().
Mocks(getUser).
Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = getUserData()
Expand All @@ -952,6 +956,8 @@ func TestApiTest_MatchesTimes(t *testing.T) {
Expect(t).
Status(http.StatusOK).
End()

assert.Empty(t, res.UnmatchedMocks())
}

type RecorderCaptor struct {
Expand Down
1 change: 1 addition & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho=
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho=
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
5 changes: 5 additions & 0 deletions mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ type MockRequest struct {
matchers []Matcher
}

// UnmatchedMock exposes some information about mocks that failed to match a request
type UnmatchedMock struct {
URL url.URL
}

// MockResponse represents the http response side of a mock interaction
type MockResponse struct {
mock *Mock
Expand Down

0 comments on commit dadd9b0

Please sign in to comment.