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

AtLeast(times int) implementation #1648

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type Call struct {
// Amount of times this call has been called
totalCalls int

// Minimum number of times this call should be called
minimumCalls int

// Call to this method can be optional
optional bool

Expand Down Expand Up @@ -88,6 +91,7 @@ func newCall(parent *Mock, methodName string, callerInfo []string, methodArgumen
ReturnArguments: make([]interface{}, 0),
callerInfo: callerInfo,
Repeatability: 0,
minimumCalls: 0,
WaitFor: nil,
RunFn: nil,
PanicMsg: nil,
Expand Down Expand Up @@ -151,6 +155,19 @@ func (c *Call) Times(i int) *Call {
return c
}

// AtLeast indicates that the mock should return at least the indicated number
// of times. The mock will always return, but the count will be verified
// when AssertExpectations() is called.
//
// Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).AtLeast(2)
func (c *Call) AtLeast(i int) *Call {
c.lock()
defer c.unlock()
c.Repeatability = 0
c.minimumCalls = i
return c
}

// WaitUntil sets the channel that will block the mock's return until its closed
// or a message is received.
//
Expand Down Expand Up @@ -644,6 +661,9 @@ func (m *Mock) checkExpectation(call *Call) (bool, string) {
if call.Repeatability > 0 {
return false, fmt.Sprintf("FAIL:\t%s(%s)\n\t\tat: %s", call.Method, call.Arguments.String(), call.callerInfo)
}
if call.minimumCalls > 0 && call.minimumCalls >= call.totalCalls {
return false, fmt.Sprintf("FAIL:\t%s(%s)\n\t\tat: %s", call.Method, call.Arguments.String(), call.callerInfo)
}
return true, fmt.Sprintf("PASS:\t%s(%s)", call.Method, call.Arguments.String())
}

Expand Down