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
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions gomock/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions gomock/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down