我正在尝试断言存根上调用了一个方法。我试图断言的方法是takes an IEnumerable<string>。我不关心确切的内容,但我只想测试计数是否为某个特定的数字。我不能得到正确的断言,我知道
Rhino.Mocks.Exceptions.ExpectationViolationException : Bob.DoThings(collection count equal to 10); Expected #1, Actual #0.我知道DoThings()确实正在被调用...我就是不能把约束弄对..
var myBob= MockRepository.GenerateStub<Bob>();
var countConstraint = Rhino.Mocks.Constraints.List.Count(Rhino.Mocks.Constraints.Is.Equal(10));
// execution code....
Joe myJoe = new Joe(myBob);
myJoe.MethodThatShouldCallDoThingWith10();
myBob.AssertWasCalled(s => s.DoThings(null), o => Constraints(countConstraint));我还尝试添加"IgnoreArguments“作为约束。我遗漏了什么?
发布于 2011-09-23 19:33:25
这里的问题是延迟执行。直到枚举了IEnumerable<string>,项目列表才会被“构建”。由于Rhino.Mocks只记录被调用的内容,所以它从不“使用”方法参数,因此,从不构建或枚举该列表。正如您所看到的,添加ToList()或ToArray()将枚举和构建列表,因此如果您使用这些方法之一,测试将会通过。
一种解决方法是获取传递给该方法的列表,并对其进行检查:
var list = (IEnumerable<int>) myBob.GetArgumentsForCallsMadeOn(b => b.DoThings(null))[0][0];
Assert.AreEqual(10, list.Count());此测试通过,不需要对您的代码进行任何更改。
发布于 2011-09-23 03:19:42
这个问题已经被Here报告过了。我已经能够用下面的Bob和Joe重现这个问题:
public interface Bob
{ void DoThings(IEnumrable<int> list); }
public class Joe
{
private readonly Bob bob;
public Joe(Bob bob)
{ this.bob = bob; }
public void MethodThatShouldCallDoThingWith10()
{
var values = Enumerable.Range(1, 100).Where(x => x > 0 && x < 11);
bob.DoThings(values);
}
}看起来Rhino Mock毕竟有一些问题,当涉及到LINQ时:要么将错误报告给Ayende,要么在您的生产代码中添加ToList() (实际上并不推荐)……
https://stackoverflow.com/questions/7519607
复制相似问题