C# Repeat.Any()似乎覆盖Repeat.Once()

C# Repeat.Any()似乎覆盖Repeat.Once(),c#,rhino-mocks,C#,Rhino Mocks,我使用的是Rhino Mocks 3.6,我设置了一个mock,我希望有一个方法第一次返回true,然后每次返回false。我是通过指定.Returntrue.Repeat.Once then.Returnfalse.Repeat.Any来实现的。但这似乎使它始终返回错误。相反,我不得不将第二个更改为.Returnfalse.Repeat.atleastone。我想知道为什么会有人这样做。下面是一些示例代码。第一次测试将失败,而第二次测试将成功 [TestClass] public class

我使用的是Rhino Mocks 3.6,我设置了一个mock,我希望有一个方法第一次返回true,然后每次返回false。我是通过指定.Returntrue.Repeat.Once then.Returnfalse.Repeat.Any来实现的。但这似乎使它始终返回错误。相反,我不得不将第二个更改为.Returnfalse.Repeat.atleastone。我想知道为什么会有人这样做。下面是一些示例代码。第一次测试将失败,而第二次测试将成功

[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();
            }
        }
    }
}
Any方法的文档如下所示:拼写和语法:

重复该方法任意次数。这有特殊的影响,因为此方法现在将忽略Ordering

因此,简而言之,Any的设计目的是忽略排序

这就提出了一个问题,您将如何设置第一个期望以返回一个结果,然后再设置其后的任何调用以返回不同的结果?似乎您能做的最好的方法是使用带有“最小”和“最大”参数的时间:

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