2009-04-21 47 views
9

我不能在我的生活中找到使用Rhino中的Fluent/AAA語法來驗證操作順序的正確語法。相當於在Rhino Mocks中使用Ordered()的AAA語法是什麼

我知道如何與老同學記錄/回放語法做到這一點:

 MockRepository repository = new MockRepository(); 
     using (repository.Ordered()) 
     { 
      // set some ordered expectations 
     } 

     using (repository.Playback()) 
     { 
      // test 
     } 

誰能告訴我,相當於這AAA語法犀牛製品將是什麼。更好的是,如果你能指出我的一些文件。

回答

6

試試這個:

// 
    // Arrange 
    // 
    var mockFoo = MockRepository.GenerateMock<Foo>(); 
    mockFoo.GetRepository().Ordered(); 
    // or mockFoo.GetMockRepository().Ordered() in later versions 

    var expected = ...; 
    var classToTest = new ClassToTest(mockFoo); 
    // 
    // Act 
    // 
    var actual = classToTest.BarMethod(); 

    // 
    // Assert 
    // 
    Assert.AreEqual(expected, actual); 
mockFoo.VerifyAllExpectations(); 
+0

謝謝,這似乎是我所需要的。 – mockobject 2009-04-21 19:03:18

4

這裏是要與交互測試的例子是什麼,你通常要使用命令預期:

// Arrange 
var mockFoo = MockRepository.GenerateMock<Foo>(); 

using(mockFoo.GetRepository().Ordered()) 
{ 
    mockFoo.Expect(x => x.SomeMethod()); 
    mockFoo.Expect(x => x.SomeOtherMethod()); 
} 
mockFoo.Replay(); //this is a necessary leftover from the old days... 

// Act 
classToTest.BarMethod 

//Assert 
mockFoo.VerifyAllExpectations(); 

這句法是非常期待/驗證,但據因爲我知道這是現在唯一的方法,它確實利用了3.5引入的一些很好的功能。

2

GenerateMock靜態助手和Ordered()沒有像我預期的那樣工作。這是沒有的伎倆,我(的關鍵似乎是明確創建你自己的MockRepository實例):

[Test] 
    public void Test_ExpectCallsInOrder() 
    { 
     var mockCreator = new MockRepository(); 
     _mockChef = mockCreator.DynamicMock<Chef>(); 
     _mockInventory = mockCreator.DynamicMock<Inventory>(); 
     mockCreator.ReplayAll(); 

     _bakery = new Bakery(_mockChef, _mockInventory); 

     using (mockCreator.Ordered()) 
     { 
      _mockInventory.Expect(inv => inv.IsEmpty).Return(false); 
      _mockChef.Expect(chef => chef.Bake(CakeFlavors.Pineapple, false)); 
     } 


     _bakery.PleaseDonate(OrderForOnePineappleCakeNoIcing); 

     _mockChef.VerifyAllExpectations(); 
     _mockInventory.VerifyAllExpectations(); 
    } 
0

tvanfosson的解決方案不爲我工作的。我需要驗證呼叫是否以特定的順序進行2次模擬。

根據Ayende的回覆Google GroupsOrdered()在AAA語法中不起作用。