如何正确使用Rhino.Mocks AssertWasCalled()?

发布于 2024-09-06 23:43:16 字数 798 浏览 4 评论 0原文

我先调用 _mocks.ReplayAll(),然后调用一个或多个 _mockedObject.AssertWasCalled(),最后调用 _mocks.VerifyAll()。但它告诉我“当模拟对象处于记录状态时,此操作无效”。

[Test]
public void SetStateExecuting_Should_Set_State_To_Pause_And_Not_Change_GlobalState_When_GlobalState_Is_Paused()
{
    var task = new Task { ID = 1, TimeZone = -660, GlobalState = TaskState.Paused };
    _taskDataProvider.Expect(p => p.StateUpdate(task.ID, task.TimeZone, TaskState.Paused));
    _mockRepository.ReplayAll();
    _manager.SetStateExecuting(task);
    _taskDataProvider.AssertWasNotCalled(p => p.GlobalStateUpdate(task.ID, 
                                                                  TaskState.Executing));
    _mockRepository.VerifyAll();
}

使这些方法正常工作的正确调用顺序是什么?

I call _mocks.ReplayAll(), then one or more _mockedObject.AssertWasCalled() and then _mocks.VerifyAll(). But it tells me that "This action is invalid when the mock object is in record state".

[Test]
public void SetStateExecuting_Should_Set_State_To_Pause_And_Not_Change_GlobalState_When_GlobalState_Is_Paused()
{
    var task = new Task { ID = 1, TimeZone = -660, GlobalState = TaskState.Paused };
    _taskDataProvider.Expect(p => p.StateUpdate(task.ID, task.TimeZone, TaskState.Paused));
    _mockRepository.ReplayAll();
    _manager.SetStateExecuting(task);
    _taskDataProvider.AssertWasNotCalled(p => p.GlobalStateUpdate(task.ID, 
                                                                  TaskState.Executing));
    _mockRepository.VerifyAll();
}

What is the correct order to call so that these methods work correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橪书 2024-09-13 23:43:16

Jon Kruger 的博客文章 “如何使用通过测试记录的 rhino 模拟” 提供了使用 rhino 模拟方法可以执行的所有操作的简单示例。他还展示了你不能做什么,我发现这对学习很有帮助。

如前所述,将 Arrange、Act、Assert 语法与静态构造函数一起使用更易于阅读。该博客文章展示了这两种方法的示例。

以下是 Jon 示例代码中的示例:

新语法:

 [Test]
    public void You_can_check_to_see_if_a_method_was_called()
    {
        var stub = MockRepository.GenerateStub<ISampleClass>();

        stub.MethodThatReturnsInteger("foo");

        stub.AssertWasCalled(s => s.MethodThatReturnsInteger("foo"));
        stub.AssertWasCalled(s => s.MethodThatReturnsInteger(Arg<string>.Is.Anything));
    }

旧样式:

    [Test]
    public void Calling_virtual_methods_will_call_the_actual_method()
    {
        var mockRepository = new MockRepository();
        var sampleClass = mockRepository.PartialMock<SampleClass>();
        sampleClass.Replay();

        sampleClass.VirtualMethod("foo").ShouldEqual(3);
        sampleClass.VirtualMethodWasCalled.ShouldBeTrue();
        sampleClass.AssertWasCalled(c => c.VirtualMethod("foo"));
    }

Jon Kruger's blog post "How to use rhino mocks documented through tests" has simple examples of everything you can do with rhino mocks methods. He also shows what you can not do which I found very helpful in learning.

As mentioned before, using the Arrange, Act, Assert Syntax with the static constructors is easier to read. The blog post shows examples of both methods.

Here are examples from Jon's sample code:

New syntax:

 [Test]
    public void You_can_check_to_see_if_a_method_was_called()
    {
        var stub = MockRepository.GenerateStub<ISampleClass>();

        stub.MethodThatReturnsInteger("foo");

        stub.AssertWasCalled(s => s.MethodThatReturnsInteger("foo"));
        stub.AssertWasCalled(s => s.MethodThatReturnsInteger(Arg<string>.Is.Anything));
    }

Old style:

    [Test]
    public void Calling_virtual_methods_will_call_the_actual_method()
    {
        var mockRepository = new MockRepository();
        var sampleClass = mockRepository.PartialMock<SampleClass>();
        sampleClass.Replay();

        sampleClass.VirtualMethod("foo").ShouldEqual(3);
        sampleClass.VirtualMethodWasCalled.ShouldBeTrue();
        sampleClass.AssertWasCalled(c => c.VirtualMethod("foo"));
    }
草莓酥 2024-09-13 23:43:16

您正在混合旧的记录/重播模式和新的 AAA 模式

在记录模式下设置“不使用这些参数调用”期望的正常方法如下所示:

_taskDataProvider.Expect(
    p => p.GlobalStateUpdate(task.ID, TaskState.Executing)).Repeat.Never();

或者,您可以使用严格的模拟,它根本不允许意外调用。

AssertWasCalledAssertWasNotCalled 适用于 AAA,您可以将断言放在末尾。使用记录/重播语法,应在切换到重播模式之前开始设置行为和期望。

AssertWasNotCalled 实际上也可能适用于记录/重放,但我从未尝试过,因为我不喜欢混合使用这两种方法的元素。它不必要地使事情变得复杂。)

You are mixing the old Record/Replay pattern and the new AAA pattern.

The normal way to set up a "not called with these arguments" expectation while in Record mode looks like this:

_taskDataProvider.Expect(
    p => p.GlobalStateUpdate(task.ID, TaskState.Executing)).Repeat.Never();

Or alternatively, you can use a strict mock which simply doesn't allow unexpected calls.

AssertWasCalled and AssertWasNotCalled are intended for AAA where you put your assertions at the end. With the Record/Replay syntax, both behavior and expecations should be set up at the start before the switch to replay mode.

(AssertWasNotCalled might actually work with Record/Replay also, but I never tried it because I don't like to mix elements from both approaches. It needlessly complicates things.)

站稳脚跟 2024-09-13 23:43:16

看来我找到了解决方案。这看起来有点奇怪,但它确实有效。事实证明,由于某种原因我需要调用 ReplayAll() 两次......

这有效:

[Test]
public void SetStateExecuting_Should_Set_State_To_Pause_And_Not_Change_GlobalState_When_GlobalState_Is_Paused()
{
    var task = new Task { ID = 1, TimeZone = -660, GlobalState = TaskState.Paused };
    _mockRepository.ReplayAll();
    _manager.SetStateExecuting(task);
    _taskDataProvider.AssertWasCalled(p => p.StateUpdate(task.ID, task.TimeZone, TaskState.Paused));
    _taskDataProvider.AssertWasNotCalled(p => p.GlobalStateUpdate(task.ID, TaskState.Executing));
    _mockRepository.ReplayAll();
}

Seems like I found the solution. It's seems a bit weird, but it works. It turns out that I need to call ReplayAll() twice for some reason...

This works:

[Test]
public void SetStateExecuting_Should_Set_State_To_Pause_And_Not_Change_GlobalState_When_GlobalState_Is_Paused()
{
    var task = new Task { ID = 1, TimeZone = -660, GlobalState = TaskState.Paused };
    _mockRepository.ReplayAll();
    _manager.SetStateExecuting(task);
    _taskDataProvider.AssertWasCalled(p => p.StateUpdate(task.ID, task.TimeZone, TaskState.Paused));
    _taskDataProvider.AssertWasNotCalled(p => p.GlobalStateUpdate(task.ID, TaskState.Executing));
    _mockRepository.ReplayAll();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文