Skip to content

Commit a0ac157

Browse files
Merge pull request #1795 from brackendawson/1227-AssertExpectationsForObjects-panic
mock.AssertExpectationsForObjects fix panic with wrong testObject type.
2 parents 5fa984a + 0bf6b94 commit a0ac157

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)