简体   繁体   English

计数方法调用Rhino Mocks

[英]Counting method calls in Rhino Mocks

So: I'd like to count method calls in Rhino Mocks with something more specific than Any(), Once() or AtLeastOnce(). 所以:我想在Rhino Mocks中使用比Any(),Once()或AtLeastOnce()更具体的方法来计算方法调用。 Is there any mechanism for doing this? 这样做有什么机制吗?

The trick is to use Repeat.Times(n), where n is the number of times. 诀窍是使用Repeat.Times(n),其中n是次数。

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) 来源: http//benbiddington.wordpress.com/2009/06/23/rhinomocks-repeat-times/ (看看那里有解释)

EDIT: only edited to summarise at the start, thanks for the useful reply. 编辑:只编辑在开始时总结,感谢有用的回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM