Rhino Mocks: Repeat.Once() not working?

故事扮演 提交于 2019-12-03 07:15:49

问题


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.


回答1:


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



回答2:


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

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



来源:https://stackoverflow.com/questions/887245/rhino-mocks-repeat-once-not-working

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