Fix panic in AssertExpectations for mocks without expectations (#1207)

Co-authored-by: Tony Abboud <tabboud@palantir.com>
pull/1198/head v1.7.4
Tony Abboud 2022-06-20 17:08:00 -04:00 committed by GitHub
parent 840cb80149
commit 48391ba5eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

@ -508,6 +508,10 @@ func (m *Mock) AssertExpectations(t TestingT) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
if m.mutex == nil {
m.mutex = &sync.Mutex{}
}
m.mutex.Lock()
defer m.mutex.Unlock()
var somethingMissing bool

View File

@ -915,6 +915,7 @@ func Test_AssertExpectationsForObjects_Helper(t *testing.T) {
var mockedService1 = new(TestExampleImplementation)
var mockedService2 = new(TestExampleImplementation)
var mockedService3 = new(TestExampleImplementation)
var mockedService4 = new(TestExampleImplementation) // No expectations does not cause a panic
mockedService1.On("Test_AssertExpectationsForObjects_Helper", 1).Return()
mockedService2.On("Test_AssertExpectationsForObjects_Helper", 2).Return()
@ -924,8 +925,8 @@ func Test_AssertExpectationsForObjects_Helper(t *testing.T) {
mockedService2.Called(2)
mockedService3.Called(3)
assert.True(t, AssertExpectationsForObjects(t, &mockedService1.Mock, &mockedService2.Mock, &mockedService3.Mock))
assert.True(t, AssertExpectationsForObjects(t, mockedService1, mockedService2, mockedService3))
assert.True(t, AssertExpectationsForObjects(t, &mockedService1.Mock, &mockedService2.Mock, &mockedService3.Mock, &mockedService4.Mock))
assert.True(t, AssertExpectationsForObjects(t, mockedService1, mockedService2, mockedService3, mockedService4))
}