首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Repeat.Any()似乎覆盖了Repeat.Once()

Repeat.Any()似乎覆盖了Repeat.Once()
EN

Stack Overflow用户
提问于 2014-07-29 17:02:50
回答 1查看 281关注 0票数 1

我正在使用Rhino 3.6,我设置了一个模拟,如果我想要一个方法第一次返回true,然后每次返回false之后。为此,我指定了.Return(true).Repeat.Once(),然后指定了.Return(false).Repeat.Any()。但这似乎让它一直都是假的。相反,我不得不将第二个改为.Return(false).Repeat.AtLeastOnce()。我想知道为什么Any()会这样做。下面是一些示例代码。第一次考试失败,第二次考试成功。

代码语言:javascript
复制
[TestClass]
public class ExampleTest
{
    private Example example;

    private IFoo foo;

    [TestInitialize]
    public void InitializeTest()
    {
        example = new Example();
        foo = MockRepository.GenerateStrictMock<IFoo>();
    }

    [TestMethod]
    public void Test1()
    {
        foo.Expect(f => f.SomeCondition()).Return(true).Repeat.Once();
        foo.Expect(f => f.SomeCondition()).Return(false).Repeat.Any();
        foo.Expect(f => f.SomeMethod()).Repeat.Once();

        example.Bar(foo);

        foo.VerifyAllExpectations();
    }

    [TestMethod]
    public void Test2()
    {
        foo.Expect(f => f.SomeCondition()).Return(true).Repeat.Once();
        foo.Expect(f => f.SomeCondition()).Return(false).Repeat.AtLeastOnce();
        foo.Expect(f => f.SomeMethod()).Repeat.Once();

        example.Bar(foo);

        foo.VerifyAllExpectations();
    }
}

public interface IFoo
{
    bool SomeCondition();

    void SomeMethod();
}

public class Example
{
    public void Bar(IFoo foo)
    {
        if (foo.SomeCondition())
        {
            if (!foo.SomeCondition())
            {
                foo.SomeMethod();
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-05 04:49:56

Any方法的文档如下(拼写和语法):

Repeat the method any number of times. This has special affects in that this method would now ignore orderring

因此,简而言之,Any被设计为忽略 ordering

这引发了这样一个问题:您将如何设置第一个期望返回一个结果,然后再任何调用来返回不同的结果?似乎您能做的最好的就是使用Times和min和max参数:

代码语言:javascript
复制
[TestMethod]
public void Test2()
{
    foo.Expect(f => f.SomeCondition()).Return(true).Repeat.Once();
    foo.Expect(f => f.SomeCondition()).Return(false).Repeat.Times(0, int.MaxValue);
    foo.Expect(f => f.SomeMethod()).Repeat.Once();

    example.Bar(foo);
    example.Bar(foo);
    example.Bar(foo);

    foo.VerifyAllExpectations();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25021001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档