所以:我想在Rhino Mocks中使用比Any(),Once()或AtLeastOnce()更具体的方法来计算方法调用.这样做有什么机制吗?
Ono*_*ots 11
诀窍是使用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();
}
Run Code Online (Sandbox Code Playgroud)
要解决此问题,请使用以下方法:
[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)
);
}
Run Code Online (Sandbox Code Playgroud)
来源:http://benbiddington.wordpress.com/2009/06/23/rhinomocks-repeat-times/ (看看那里有解释)
编辑:只编辑在开始时总结,感谢有用的回复.
| 归档时间: |
|
| 查看次数: |
4801 次 |
| 最近记录: |