Rhino Mock List Limit

I am trying to claim that the method was called on the stub. The method I'm trying to claim is called IEnumerable<string> . I don't care about the exact content, but I just want to check that the count is a specific number. I can’t get the statement right, I get

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

I know that DoThings () is really being called ... I just can't get the constraint correctly.

 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)); 

I also tried adding “IgnoreArguments” as a limitation. What am I missing?

+7
source share
2 answers

The problem here is with delayed execution. This is not until the IEnumerable<string> is listed that the list of elements is “built”. Because Rhino.Mocks simply records what is called, it never "uses" the method arguments, and therefore the list is never built or enumerated. As you saw, adding ToList () or ToArray () lists and creates a list so that the test passes if you use one of these methods.

One workaround is to grab the list that was passed to the method and do your check:

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

This test passes and does not require any changes to your code.

+11
source

This issue has been reported here . I was able to reproduce this problem with the following Bob and 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); } } 

There seems to be some kind of problem in Rhino Mocks when it comes to LINQ: either report an error in Ayende , or add ToList () in your production code (not recommended) ...

+2
source

All Articles