RhinoMocks 组上已经有一个线程。
GenerateMock 创建一个动态模拟。动态模拟允许未指定(=预期)的调用。如果发生这种情况,它只会返回 null(或返回类型的默认值)。
注意:重复是行为的规范(如 Stub),而不是期望,即使在期望中指定。
如果你想避免超过一定数量的调用,你可以写:
[Test]
public void uhh_what()
{
var a = MockRepository.GenerateMock<IPrebuiltNotifier>();
a.Expect(x => x.Notify()).Repeat.Once();
a.Stub(x => x.Notify()).Throw(new InvalidOperationException("gotcha"));
a.Notify();
// this fails
a.Notify();
a.VerifyAllExpectations();
}
或者
[Test]
public void uhh_what()
{
var a = MockRepository.GenerateMock<IPrebuiltNotifier>();
a.Notify();
a.Notify();
// this fails
a.AssertWasCalled(
x => x.Notify(),
o => o.Repeat.Once());
}