Moq First() Last() 和 GetEnumerator() 怪异
我是莫清,我的路线部分来自 我的路线的rps = new List
... (3 Route Parts)
和 Moqing GetEnumerator()
如下
route.Setup( ro => ro.GetEnumerator()).Returns(rps.GetEnumerator());
但 Moq
在以下代码中失败,并在调用时显示“序列不包含元素” Last()
o.Route.Any(rp => rp.IsNonTowLocation &&
rp != o.Route.First() &&
rp != o.Route.Last())
在直接窗口中查看 First()
Last()
,我发现如果执行 First(),值就会发生变化
Last()
多次。就好像 MoveNext()
被调用,但 Reset()
没有被调用,而 Enumerable 完全混淆了。有没有人在 Moq
中遇到过这种情况并找到了解决方案?
I am Moqing my Route Parts from arps = new List
... (3 Route Parts)
and Moqing GetEnumerator()
for my Route as below
route.Setup(ro => ro.GetEnumerator()).Returns(rps.GetEnumerator());
but the Moq
fails in the following code with "Sequence contains no elements" on the call to Last()
o.Route.Any(rp => rp.IsNonTowLocation &&
rp != o.Route.First() &&
rp != o.Route.Last())
Looking at First()
Last()
in the immediate windows I find the values change if I execute First()
Last()
multiple times. Its as if the MoveNext()
gets called but not Reset()
and the Enumerable it totally confused. Has anyone experienced this with Moq
and found a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经设置了 GetEnumerator,以便它每次都返回相同的枚举器实例。
这相当于:
如果你想在每次调用时使用一个新的枚举器,那么你需要传递返回一个 lambda 表达式:
每次调用 GetEnumerator() 时都会调用 lambda - 所以
First()
和然后Last()
应该按预期工作。You've setup your GetEnumerator so that it returns the same enumerator instance every time.
This is equivalent to:
If you want a new enumerator on every call, then you need to pass Returns a lambda expression:
The lambda will get called every time GetEnumerator() is called - so
First()
andLast()
should then work as expected.