Reputation: 16209
There is a IEnumerable<IDomainInterface>
passed into my class and I want to test whether a specific method was called for every item:
// in class
var result = _items.SelectMany (x => x.Get (something));
// in test
domainInterfaceMock.Expect(x => x.Get (something));
The test only passes if I append ToArray()
to the SelectMany()
statement...
How should I deal with such situations?
Upvotes: 1
Views: 343
Reputation: 14783
This is because Get() is only called when the LINQ expression is evaluated (i.e. iterated over). If your method in your class is calling Get() it must be doing something more than just
var result = _items.SelectMany (x => x.Get (something));
... as this in itself will not result in any calls to Get(). You should modify the test to reflect this.
Update: You're testing the method, so you should mock up the inputs to the method and pass them to the actual method, and test the expectations on these inputs and the result. If the method evaluates the LINQ expression itself then you'll find it calls Get(), however if it doesn't evaluate the LINQ expression itself you're doing the right thing by forcing the evaluation with a .ToArray().
More accurately by calling .ToArray() you're trying to demonstrate the return value of the method was what you expected, but to do this you directly you would have to analyse the returned expression. If the only fact you want to prove is that the expression calls Get() for all its members then what you're doing achieves that.
Upvotes: 1
Reputation: 5551
If x is mockable (I.e. It's an interface or abstract class) then you could verify that for every mocked instance of x that you create the Get() method is called.
Upvotes: 0