What is the AAA syntax equivalent to using Ordered() in Rhino Mocks

时光怂恿深爱的人放手 提交于 2019-11-30 18:43:12
tvanfosson

Try this:

  //
  // 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();

Here's an example with interaction testing which is what you usually want to use ordered expectations for:

// 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();

This syntax is very much Expect/Verify but as far as I know it's the only way to do it right now, and it does take advantage of some of the nice features introduced with 3.5.

The GenerateMock static helper along with Ordered() did not work as expected for me. This is what did the trick for me (the key seems to be to explicitly create your own MockRepository instance):

    [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();
    }

tvanfosson's solution does not work for me either. I needed to verify that calls are made in particular order for 2 mocks.

According to Ayende's reply in Google Groups Ordered() does not work in AAA syntax.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!