[Test]
public void MockAGenericInterface()
{
MockRepository mocks = new MockRepository();
IList<int> list = mocks.Create Mock<IList<int>>();
Assert.IsNotNull(list);
Expect.Call(list.Count).Return(5);
mocks.ReplayAll();
Assert.AreEqual(5, list.Count);
mocks.VerifyAll();
}
此代码中ReplayAll()
和VerifyAll()
的目的是什么?
答案 0 :(得分:19)
代码段演示了Rhino.Mocks的记录/重播/验证语法。您首先记录模拟的期望(使用Expect.Call()
,然后调用ReplayAll()
来运行模拟模拟。然后,您调用VerifyAll()
以验证是否已满足所有期望。< / p>
顺便说一句,这是一种过时的语法。新语法称为AAA Syntax - Arrange, Act, Assert,通常比旧的R / R / V语言更容易使用。您将代码剪切转换为AAA:
[Test]
public void MockAGenericInterface()
{
IList<int> list = MockRepository.GenerateMock<IList<int>>();
Assert.IsNotNull(list);
list.Expect (x => x.Count).Return(5);
Assert.AreEqual(5, list.Count);
list.VerifyAllExpectations();
}