Rhino Mocks: Repeat.Once() not working?

情到浓时终转凉″ 提交于 2019-12-02 20:48:22

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());
}

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!