Skip to content

Commit

Permalink
Modify AssertCalled and AssertNotCalled to give better error messages
Browse files Browse the repository at this point in the history
Co-authored-by: Giuseppe Landolfi <giuseppe.landolfi@ricardo.ch>
Co-authored-by: Ernesto Jiménez <me@ernesto-jimenez.com>
  • Loading branch information
3 people committed Mar 3, 2018
1 parent bfc7630 commit 0bfbef4
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,17 @@ func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls
func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interface{}) bool {
m.mutex.Lock()
defer m.mutex.Unlock()
if !assert.True(t, m.methodWasCalled(methodName, arguments), fmt.Sprintf("The \"%s\" method should have been called with %d argument(s), but was not.", methodName, len(arguments))) {
t.Logf("%v", m.expectedCalls())
return false
if !m.methodWasCalled(methodName, arguments) {
var calledWithArgs []string
for _, call := range m.calls() {
calledWithArgs = append(calledWithArgs, fmt.Sprintf("%v", call.Arguments))
}
if len(calledWithArgs) == 0 {
return assert.Fail(t, "Should have called with given arguments",
fmt.Sprintf("Expected %q to have been called with:\n%v\nbut no actual calls happened", methodName, arguments))
}
return assert.Fail(t, "Should have called with given arguments",
fmt.Sprintf("Expected %q to have been called with:\n%v\nbut actual calls were:\n %v", methodName, arguments, strings.Join(calledWithArgs, "\n")))
}
return true
}
Expand All @@ -459,9 +467,9 @@ func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interfac
func (m *Mock) AssertNotCalled(t TestingT, methodName string, arguments ...interface{}) bool {
m.mutex.Lock()
defer m.mutex.Unlock()
if !assert.False(t, m.methodWasCalled(methodName, arguments), fmt.Sprintf("The \"%s\" method was called with %d argument(s), but should NOT have been.", methodName, len(arguments))) {
t.Logf("%v", m.expectedCalls())
return false
if m.methodWasCalled(methodName, arguments) {
return assert.Fail(t, "Should not have called with given arguments",
fmt.Sprintf("Expected %q to not have been called with:\n%v\nbut actually it was.", methodName, arguments))
}
return true
}
Expand Down

0 comments on commit 0bfbef4

Please sign in to comment.