所以:我想用比 An()、Ounce() 或 At Least Once() 更具体的方法来计算 Rhino Mocks 中的方法调用。有什么机制可以做到这一点吗?
诀窍是使用 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)
);
}
Source: http://benbiddington.wordpress.com/2009/06/23/rhinomocks-repeat-times/ http://benbiddington.wordpress.com/2009/06/23/rhinomocks-repeat-times/(在那里寻找解释)
编辑:仅在开始时进行总结,感谢您的有用回复。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)