什么是RhinoMocks中的ReplayAll()和VerifyAll()

Sai*_*and 8 c# rhino-mocks

[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();
}
Run Code Online (Sandbox Code Playgroud)

是什么目的ReplayAll(),并VerifyAll()在此代码?

Igo*_*ejc 19

代码片段演示了Rhino.Mocks 的Record/Replay/Verify语法.您首先记录模拟的期望(使用Expect.Call(),然后调用ReplayAll()运行模拟模拟.然后,您调用VerifyAll()以验证是否已满足所有期望.

顺便说一句,这是一种过时的语法.新语法称为AAA语法 - 排列,操作,断言,并且通常比旧的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();
  }
Run Code Online (Sandbox Code Playgroud)