排序方法的返回值与犀牛,模拟存根 [英] Ordering method return values with Rhino-Mock stubs

查看:151
本文介绍了排序方法的返回值与犀牛,模拟存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始与犀牛,嘲笑(3.6),而读罗伊Osherove的的单元测试的艺术实验。他演示了一个模拟的方法可以编写脚本返回不同的结果与相同的参数调用两次时的例子:

I've started experimenting with Rhino-Mocks (3.6) while reading Roy Osherove's The Art of Unit Testing. He has an example that demonstrates that a mocked method can be scripted to return different results when called twice with the same parameter:

   [Test]
    public void ReturnResultsFromMock()
    {
        MockRepository repository = new MockRepository();
        IGetRestuls resultGetter = repository.DynamicMock<IGetRestuls>();
        using(repository.Record())
        {
            resultGetter.GetSomeNumber("a");
            LastCall.Return(1);

            resultGetter.GetSomeNumber("a");
            LastCall.Return(2);

            resultGetter.GetSomeNumber("b");
            LastCall.Return(3);

        }

        int result = resultGetter.GetSomeNumber("b");
        Assert.AreEqual(3, result);

        int result2 = resultGetter.GetSomeNumber("a");
        Assert.AreEqual(1, result2);

        int result3 = resultGetter.GetSomeNumber("a");
        Assert.AreEqual(2, result3);
    }

这工作正常。但是,当我尝试用存根,并接受并返回一个字符串的方法同样的事情,我不能够产生第二返回值:

This works fine. But when I try the same thing with a Stub, and a method that accepts and returns a string, I am not able to generate the second return value:

    [Test]
    public void StubMethodWithStringParameter_ScriptTwoResponses_SameResponseReceived()
    {
        MockRepository mocks = new MockRepository();
        IMessageProvider stub = mocks.Stub<IMessageProvider>();

        using (mocks.Record())
        {
            stub.GetMessageForValue("a");
            LastCall.Return("First call");
            stub.GetMessageForValue("a");
            LastCall.Return("Second call");
        }

        Assert.AreEqual("First call", stub.GetMessageForValue("a"));
        Assert.AreEqual("Second call", stub.GetMessageForValue("a"));
    }
}

public interface IMessageProvider
{
    string GetMessage();
    string GetMessageForValue(string value);
}

本测试失败,因为First Call调查收到两个电话。我试过语法的几个皱纹(使用mocks.Ordered()的setResult,期待等),但我仍然无法得到第二次的结果出现。

This test is failing, because "First Call" is received for both calls. I've tried several wrinkles of syntax (Using mocks.Ordered(), SetResult, Expect etc.), but am still unable to get the second result to appear.

我做得不对,或者这是犀牛,嘲笑的限制?我检查这个博客文章,但建议的语法没有。解决我的问题。

Am I doing something wrong, or is this a limitation with Rhino-Mocks? I've checked this blog post, but the suggested syntax did not resolve my issue.

推荐答案

您错过此位告诉存根的第一个值只应立即退还:

The bit you're missing is to tell the stub that the first value should only be returned once:

...
using (mocks.Record())
{
    stub.GetMessageForValue("a");
    LastCall.Return("First call").Repeat.Once();
    stub.GetMessageForValue("a");
    LastCall.Return("Second call");
}



当然你的第二个电话的真正含义第二或-后续调用除非你强加与重复其他限制

Of course your "Second call" really means "Second-or-subsequent call" unless you impose other restrictions with Repeat.

您还可以考虑使用较新的安排,法,断言(的 AAA )语法RhinoMocks的现在提供:

You might also consider using the newer Arrange, Act, Assert (AAA) syntax RhinoMocks now offers:

[Test]
public void StubMethodWithStringParameter_ScriptTwoResponses_SameResponseReceived()
{
    IMessageProvider stub = MockRepository.GenerateStub<IMessageProvider>();

    stub.Expect(mp => mp.GetMessageForValue("a"))
        .Return("First call")
        .Repeat.Once();
    stub.Expect(mp => mp.GetMessageForValue("a"))
        .Return("Second call");

    Assert.AreEqual("First call", stub.GetMessageForValue("a"));
    Assert.AreEqual("Second call", stub.GetMessageForValue("a"));
}



这是一个小更简洁,通常不必担心记录您节省 - 播放断言存根的状态。德里克·贝利写了文章有关使用重复上洛技术高手。这也恰好使用AAA语法)。

It's a little more concise and generally saves you from having to worry about the record-playback-assert state of the stub. Derick Bailey wrote an article about using Repeat on Los Techies. It also happens to use the AAA syntax).

这篇关于排序方法的返回值与犀牛,模拟存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆