Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic in AssertExpectations for mocks without expectations #1207

Merged
merged 1 commit into from Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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