我正在使用Rhino 3.6,我设置了一个模拟,如果我想要一个方法第一次返回true,然后每次返回false之后。为此,我指定了.Return(true).Repeat.Once(),然后指定了.Return(false).Repeat.Any()。但这似乎让它一直都是假的。相反,我不得不将第二个改为.Return(false).Repeat.AtLeastOnce()。我想知道为什么Any()会这样做。下面是一些示例代码。第一次考试失败,第二次考试成功。
[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();
}
}
}
}发布于 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参数:
[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();
}https://stackoverflow.com/questions/25021001
复制相似问题