Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Add AtLeastOnce() method #22

Closed
wants to merge 1 commit into from
Closed
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
Add AtLeastOnce() method
Allows a test to ensure that an call happens at least
one time, but doesn't care how many more times a call occurs.
Daniel Zepeda committed Jan 20, 2016
commit bb3054b65b50fbc7f4953fc8632ce8b1fe054ea2
5 changes: 5 additions & 0 deletions gomock/call.go
Original file line number Diff line number Diff line change
@@ -46,6 +46,11 @@ func (c *Call) AnyTimes() *Call {
return c
}

func (c *Call) AtLeastOnce() *Call {
c.minCalls, c.maxCalls = 1, 1e8 // close enough to infinity
return c
}

// Do declares the action to run when the call is matched.
// It takes an interface{} argument to support n-arity functions.
func (c *Call) Do(f interface{}) *Call {
26 changes: 26 additions & 0 deletions gomock/controller_test.go
Original file line number Diff line number Diff line change
@@ -210,6 +210,32 @@ func TestAnyTimes(t *testing.T) {
ctrl.Finish()
}

func TestAtLeastOnce(t *testing.T) {
// It fails if there are no calls
reporter, ctrl := createFixtures(t)
subject := new(Subject)
ctrl.RecordCall(subject, "FooMethod", "argument").AtLeastOnce()
reporter.assertFatal(func() {
ctrl.Finish()
})

// It succeeds if there is one call
reporter, ctrl = createFixtures(t)
subject = new(Subject)
ctrl.RecordCall(subject, "FooMethod", "argument").AtLeastOnce()
ctrl.Call(subject, "FooMethod", "argument")
ctrl.Finish()

// It succeeds if there are many calls
reporter, ctrl = createFixtures(t)
subject = new(Subject)
ctrl.RecordCall(subject, "FooMethod", "argument").AtLeastOnce()
for i := 0; i < 100; i++ {
ctrl.Call(subject, "FooMethod", "argument")
}
ctrl.Finish()
}

func TestDo(t *testing.T) {
_, ctrl := createFixtures(t)
subject := new(Subject)