Rhino Mock List约束

Ste*_*bbi 7 c# unit-testing rhino-mocks list

我试图断言在存根上调用了一个方法.我试图断言的方法被称为IEnumerable<string>.我不关心确切的内容,但我只想测试计数是一定数量.我知道,我无法得到断言

Rhino.Mocks.Exceptions.ExpectationViolationException : Bob.DoThings(collection count equal to 10); Expected #1, Actual #0.
Run Code Online (Sandbox Code Playgroud)

我知道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));
Run Code Online (Sandbox Code Playgroud)

我也尝试添加"IgnoreArguments"作为约束.我错过了什么?

Pat*_*ele 11

这里的问题是延迟执行.直到IEnumerable<string>枚举后,项目列表才"构建".由于Rhino.Mocks只记录被调用的内容,因此它永远不会"使用"方法参数,因此,列表永远不会被构建也不会被枚举.如您所见,添加ToList()或ToArray()枚举并构建列表,以便在使用其中一种方法时测试将通过.

一种解决方法是获取传递给方法的列表并对其进行检查:

var list = (IEnumerable<int>) myBob.GetArgumentsForCallsMadeOn(b => b.DoThings(null))[0][0];
Assert.AreEqual(10, list.Count());
Run Code Online (Sandbox Code Playgroud)

此测试通过并且不需要对代码进行任何更改.