Skip to content

Commit 0bf6b94

Browse files
committed
mock.AssertExpectationsForObjects fix panic with wrong testObject type.
Previously passing a mock.Mock by value rather than by reference worked (in cases without mutex locking issues) and logged a warning. This was broken by #1212 which introduced a breaking change in an attempt to fix go vet. There is no clean way to fix the breaking change as we now have (and want) go vet in our CI. This PR does not revert the breaking change but changes the panic to a test failure with a useful message.
1 parent 5fa984a commit 0bf6b94

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

mock/mock.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,11 +603,12 @@ func AssertExpectationsForObjects(t TestingT, testObjects ...interface{}) bool {
603603
h.Helper()
604604
}
605605
for _, obj := range testObjects {
606-
if m, ok := obj.(*Mock); ok {
607-
t.Logf("Deprecated mock.AssertExpectationsForObjects(myMock.Mock) use mock.AssertExpectationsForObjects(myMock)")
608-
obj = m
606+
m, ok := obj.(assertExpectationiser)
607+
if !ok {
608+
t.Errorf("Invalid test object type %T. Expected reference to a mock.Mock, eg: 'AssertExpectationsForObjects(t, myMock)' or 'AssertExpectationsForObjects(t, &myMock.Mock)'", obj)
609+
continue
610+
609611
}
610-
m := obj.(assertExpectationiser)
611612
if !m.AssertExpectations(t) {
612613
t.Logf("Expectations didn't match for Mock: %+v", reflect.TypeOf(m))
613614
return false

mock/mock_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,3 +2461,9 @@ func TestIssue1785ArgumentWithMutatingStringer(t *testing.T) {
24612461
m.MethodCalled("Method", &mutatingStringer{N: 2})
24622462
m.AssertExpectations(t)
24632463
}
2464+
2465+
func TestIssue1227AssertExpectationsForObjectsWithMock(t *testing.T) {
2466+
mockT := &MockTestingT{}
2467+
AssertExpectationsForObjects(mockT, Mock{})
2468+
assert.Equal(t, 1, mockT.errorfCount)
2469+
}

0 commit comments

Comments
 (0)