Counting method calls in Rhino Mocks

独自空忆成欢 提交于 2019-12-04 20:43:38

问题


So: I'd like to count method calls in Rhino Mocks with something more specific than Any(), Once() or AtLeastOnce(). Is there any mechanism for doing this?


回答1:


The trick is to use Repeat.Times(n), where n is the number of times.

Suprisingly the below test will pass, even if the method is called more often than expected:

[Test]
public void expect_repeat_n_times_does_not_work_when_actual_greater_than_expected() {
  const Int32 ActualTimesToCall = 6;
  const Int32 ExpectedTimesToCall = 4;

  var mock = MockRepository.GenerateMock<IExample>();
  mock.Expect(example => example.ExampleMethod()).Repeat.Times(ExpectedTimesToCall);

  for (var i = 0; i < ActualTimesToCall; i++) {
      mock.ExampleMethod();
  }

  // [?] This one passes
  mock.VerifyAllExpectations();
}

To work around this use the below method:

[Test]
public void aaa_repeat_n_times_does_work_when_actual_greater_than_expected() {
  const Int32 ActualTimesToCall = 6;
  const Int32 ExpectedTimesToCall = 4;

  var mock = MockRepository.GenerateMock<IExample>();

  for (var i = 0; i < ActualTimesToCall; i++) {
      mock.ExampleMethod();
  }

  // This one fails (as expected)
  mock.AssertWasCalled(
      example => example.ExampleMethod(),
      options => options.Repeat.Times(ExpectedTimesToCall)
  );
}

Source: http://benbiddington.wordpress.com/2009/06/23/rhinomocks-repeat-times/ (look there for an explanation)

EDIT: only edited to summarise at the start, thanks for the useful reply.



来源:https://stackoverflow.com/questions/6843460/counting-method-calls-in-rhino-mocks

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