Skip to content

Commit

Permalink
Fix panic in AssertExpectations for mocks without expectations (#1207)
Browse files Browse the repository at this point in the history
Co-authored-by: Tony Abboud <tabboud@palantir.com>
  • Loading branch information
tabboud and Tony Abboud committed Jun 20, 2022
1 parent 840cb80 commit 48391ba
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 4 additions & 0 deletions mock/mock.go
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions mock/mock_test.go
Expand Up @@ -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()
Expand All @@ -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))

}

Expand Down

0 comments on commit 48391ba

Please sign in to comment.