Skip to content

Commit 48391ba

Browse files
tabboudTony Abboud
and
Tony Abboud
authored
Fix panic in AssertExpectations for mocks without expectations (#1207)
Co-authored-by: Tony Abboud <[email protected]>
1 parent 840cb80 commit 48391ba

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

mock/mock.go

+4
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,10 @@ func (m *Mock) AssertExpectations(t TestingT) bool {
508508
if h, ok := t.(tHelper); ok {
509509
h.Helper()
510510
}
511+
if m.mutex == nil {
512+
m.mutex = &sync.Mutex{}
513+
}
514+
511515
m.mutex.Lock()
512516
defer m.mutex.Unlock()
513517
var somethingMissing bool

mock/mock_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ func Test_AssertExpectationsForObjects_Helper(t *testing.T) {
915915
var mockedService1 = new(TestExampleImplementation)
916916
var mockedService2 = new(TestExampleImplementation)
917917
var mockedService3 = new(TestExampleImplementation)
918+
var mockedService4 = new(TestExampleImplementation) // No expectations does not cause a panic
918919

919920
mockedService1.On("Test_AssertExpectationsForObjects_Helper", 1).Return()
920921
mockedService2.On("Test_AssertExpectationsForObjects_Helper", 2).Return()
@@ -924,8 +925,8 @@ func Test_AssertExpectationsForObjects_Helper(t *testing.T) {
924925
mockedService2.Called(2)
925926
mockedService3.Called(3)
926927

927-
assert.True(t, AssertExpectationsForObjects(t, &mockedService1.Mock, &mockedService2.Mock, &mockedService3.Mock))
928-
assert.True(t, AssertExpectationsForObjects(t, mockedService1, mockedService2, mockedService3))
928+
assert.True(t, AssertExpectationsForObjects(t, &mockedService1.Mock, &mockedService2.Mock, &mockedService3.Mock, &mockedService4.Mock))
929+
assert.True(t, AssertExpectationsForObjects(t, mockedService1, mockedService2, mockedService3, mockedService4))
929930

930931
}
931932

0 commit comments

Comments
 (0)