Rhino Mocks 中使用 Ordered() 的 AAA 语法等价于什么?

10

我无论如何都找不到使用Rhino中Fluent/AAA语法验证操作顺序的正确语法。

我知道如何使用老式的录制/回放语法来完成这个任务:

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

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

有人能告诉我如何用 Rhino Mocks 的 AAA 语法实现这个功能的等效代码吗?更好的是,如果你能指向一些相关文档就更好了。

4个回答

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

谢谢,这似乎是我需要的。 - mockobject
这对我没用。我错过了什么吗?我在这个帖子中发布了对我有用的内容作为回应。 - Gishu
你使用的是哪个版本的Rhino Mocks? - tvanfosson

5
这里有一个与交互测试相关的例子,通常你会使用有序期望(ordered expectations):

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

这种语法非常类似于Expect/Verify,但据我所知,现在这是唯一的方法,并且它利用了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 Groups中的回复,Ordered()在AAA语法中不起作用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接