Rhino 模拟:存根 IEnumerable时 IEnumerator.MoveNext() 出现问题界面
我们的 API 有一个继承自 IEnumerable 的接口,如下所示:
public interface IFoos : IEnumerable<IFoo>
{
// Additional foo-collection-specific methods
}
我正在为依赖于 IFoos 的类编写单元测试。它有一个在 IFoos 上迭代的方法,如下所示:
public class Bar
{
public IFoos Foos {get; set;}
public void FooItUp()
{
foreach (IFoo foo in this.Foos)
{
foo.JumpAround();
}
}
}
在我的单元测试中,我设法消除依赖关系并使用 Rhino Mocks 返回一个枚举器,如下所示:
[Test]
public void FooItUp_JumpsUpJumpsUpAndGetsDown()
{
// Arrange
var mocks = new MockRepository();
var stubFoo1 = mocks.Stub<IFoo>();
var stubFoo2 = mocks.Stub<IFoo>();
var stubFoos = mockRepository.Stub<IFoos>().
var fooEnumerator = new List<IFoo> { stubFoo1, stubFoo2 }.GetEnumerator();
stubFoos.Stub(x => x.GetEnumerator()).Return(null).WhenCalled(x => x.ReturnValue = fooEnumerator);
Bar bar = new bar();
bar.Foos = stubFoos;
// Act
bar.FooItUp();
// Assert
...
}
当我尝试运行此测试时,会抛出异常
System.InvalidOperationException : Previous method 'IEnumerator.MoveNext();' requires a return value or an exception to throw.
:不过,我应该实现 IEnumerable 接口或为其设置返回值的唯一方法是我上面所做的 GetEnumerator(),对吧?为什么 Bar.FooItUp() 中的 foreach 循环不直接调用 List 枚举器上的 MoveNext() ?
Our API has an interface inheriting from IEnumerable like this:
public interface IFoos : IEnumerable<IFoo>
{
// Additional foo-collection-specific methods
}
I'm writing a unit test for a class that depends on IFoos. It has a method that iterates on an IFoos, like this:
public class Bar
{
public IFoos Foos {get; set;}
public void FooItUp()
{
foreach (IFoo foo in this.Foos)
{
foo.JumpAround();
}
}
}
In my unit test, I manage to stub out the dependency and return an Enumerator with Rhino Mocks like so:
[Test]
public void FooItUp_JumpsUpJumpsUpAndGetsDown()
{
// Arrange
var mocks = new MockRepository();
var stubFoo1 = mocks.Stub<IFoo>();
var stubFoo2 = mocks.Stub<IFoo>();
var stubFoos = mockRepository.Stub<IFoos>().
var fooEnumerator = new List<IFoo> { stubFoo1, stubFoo2 }.GetEnumerator();
stubFoos.Stub(x => x.GetEnumerator()).Return(null).WhenCalled(x => x.ReturnValue = fooEnumerator);
Bar bar = new bar();
bar.Foos = stubFoos;
// Act
bar.FooItUp();
// Assert
...
}
When I try running this test, an exception is thrown:
System.InvalidOperationException : Previous method 'IEnumerator.MoveNext();' requires a return value or an exception to throw.
Looking at the IEnumerable interface, though, the only method I should have to implement or set a return value for is GetEnumerator() which I did above, right? Why doesn't the foreach loop in Bar.FooItUp() just call MoveNext() on the List enumerator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
杜尔。
在对它们进行操作之前,我没有将存根置于重播状态。
在调用 bar.FooItUp() 之前添加行:
解决了该问题。
Durrr.
I wasn't putting my stubs in replay state before acting on them.
Adding the line:
before calling bar.FooItUp() fixed the issue.
尝试更改:
至:
编辑:您链接的问题中的有趣信息。
看完之后,尝试一下:
Try changing:
To:
Edit: interesting info in that question you linked.
Having looked at it, try: