diff --git a/gomock/call.go b/gomock/call.go index ea1e0922..76621b5f 100644 --- a/gomock/call.go +++ b/gomock/call.go @@ -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 { diff --git a/gomock/controller_test.go b/gomock/controller_test.go index acd8d71c..8ad942a7 100644 --- a/gomock/controller_test.go +++ b/gomock/controller_test.go @@ -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)