所以:我想在Rhino Mocks中使用比Any(),Once()或AtLeastOnce()更具体的方法来计算方法调用。有没有这样做的机制?
答案 0 :(得分:10)
技巧是使用Repeat.Times(n),其中n是次数。
令人惊讶的是,即使比预期更频繁地调用该方法,下面的测试也会通过:
[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();
}
要解决此问题,请使用以下方法:
[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/ (看那里解释)
编辑:只编辑在开始时总结,感谢有用的回复。