George Mauer
George Mauer

Reputation: 122172

Rhino Mocks: Repeat.Once() not working?

Can anyone tell me why in the world the following test is not failing?

[Test]
public void uhh_what() {
    var a = MockRepository.GenerateMock<IPrebuiltNotifier>();
    a.Expect(x => x.Notify()).Repeat.Once();
    a.Notify();
    a.Notify();
    a.VerifyAllExpectations();
}

Really need a second pair of eyes to confirm I'm not crazy...now I'm worried that all my tests are unreliable.

Upvotes: 15

Views: 5820

Answers (2)

Stefan Steinegger
Stefan Steinegger

Reputation: 64628

There is already a thread on the RhinoMocks group.

GenerateMock creates a dynamic mock. The dynamic mock allows calls that are not specified (=expected). If this happens, it just returns null (or the default value of the return type).

Note: Repeat is a specification of the behaviour (like Stub), not the expectation even if specified in an expectation.

If you want to avoid having more then a certain number of calls, you could write:

[Test]
public void uhh_what() 
{
    var a = MockRepository.GenerateMock<IPrebuiltNotifier>();
    a.Expect(x => x.Notify()).Repeat.Once();
    a.Stub(x => x.Notify()).Throw(new InvalidOperationException("gotcha"));
    a.Notify();

    // this fails
    a.Notify();

    a.VerifyAllExpectations();
}

Or

[Test]
public void uhh_what() 
{
    var a = MockRepository.GenerateMock<IPrebuiltNotifier>();
    a.Notify();
    a.Notify();

    // this fails
    a.AssertWasCalled(
      x => x.Notify(), 
      o => o.Repeat.Once());
}

Upvotes: 27

Sam Shiles
Sam Shiles

Reputation: 11259

When using GenerateMock (or with Dynamic Mocks in general) I always mentally insert the following:

a.Expect(x => x.Notify()).Repeat.*[AtLeast]*Once();

Upvotes: 9

Related Questions