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

mock: add assertion for minimum number of method/function invocations #1595

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,22 @@ func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls
return assert.Equal(t, expectedCalls, actualCalls, fmt.Sprintf("Expected number of calls (%d) does not match the actual number of calls (%d).", expectedCalls, actualCalls))
}

// AssertMinNumberOfCalls asserts that the method was called at-least expectedMinCalls times.
func (m *Mock) AssertMinNumberOfCalls(t TestingT, methodName string, expectedMinCalls int) bool {
if h, ok := t.(tHelper); ok {
h.Helper()
}
m.mutex.Lock()
defer m.mutex.Unlock()
var actualCalls int
for _, call := range m.calls() {
if call.Method == methodName {
actualCalls++
}
}
return assert.LessOrEqual(t, expectedMinCalls, actualCalls, fmt.Sprintf("Actual number of calls (%d) does not satisfy the minimum expected number of calls (%d).", actualCalls, expectedMinCalls))
}

// AssertCalled asserts that the method was called.
// It can produce a false result when an argument is a pointer type and the underlying value changed after calling the mocked method.
func (m *Mock) AssertCalled(t TestingT, methodName string, arguments ...interface{}) bool {
Expand Down
16 changes: 16 additions & 0 deletions mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,22 @@ func Test_Mock_AssertNumberOfCalls(t *testing.T) {

}

func Test_Mock_AssertMinNumberOfCalls(t *testing.T) {

var mockedService = new(TestExampleImplementation)

mockedService.On("Test_Mock_AssertMinNumberOfCalls", 1, 2, 3).Return(5, 6, 7)

mockedService.Called(1, 2, 3)
assert.True(t, mockedService.AssertNumberOfCalls(t, "Test_Mock_AssertMinNumberOfCalls", 1))
assert.True(t, mockedService.AssertMinNumberOfCalls(t, "Test_Mock_AssertMinNumberOfCalls", 1))

mockedService.Called(1, 2, 3)
assert.True(t, mockedService.AssertNumberOfCalls(t, "Test_Mock_AssertMinNumberOfCalls", 2))
assert.True(t, mockedService.AssertMinNumberOfCalls(t, "Test_Mock_AssertMinNumberOfCalls", 1))
assert.True(t, mockedService.AssertMinNumberOfCalls(t, "Test_Mock_AssertMinNumberOfCalls", 2))
}

func Test_Mock_AssertCalled(t *testing.T) {

var mockedService = new(TestExampleImplementation)
Expand Down