Rhino Mocks 中的计数方法调用 [英] Counting method calls in Rhino Mocks
问题描述
所以:我想用比 Any()、Once() 或 AtLeastOnce() 更具体的东西来计算 Rhino Mocks 中的方法调用.有什么机制可以做到这一点吗?
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?
推荐答案
诀窍是使用 Repeat.Times(n),其中 n 是次数.
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)
);
}
来源:http://benbiddington.wordpress.com/2009/06/23/rhinomocks-repeat-times/(在那里找解释)
Source: http://benbiddington.wordpress.com/2009/06/23/rhinomocks-repeat-times/ (look there for an explanation)
一开始只编辑了总结,感谢有用的回复.
only edited to summarise at the start, thanks for the useful reply.
这篇关于Rhino Mocks 中的计数方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!