2011-05-20 38 views
8

什麼是ReplayAll()和VerifyAll()在RhinoMocks

[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()的目的是什麼?

回答

19

該代碼片段演示了Rhino.Mocks的記錄/重放/驗證語法。你先錄製一個模擬(使用Expect.Call()的期望,然後調用ReplayAll()運行模擬仿真。然後,你叫VerifyAll()驗證所有的期望都得到滿足。

這是一個過時的語法,順便說一句。新的語法叫做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(); 
    }