Now my question is if it is possible to substitute the methods of IQueryable with NSubstitute?
Any
isn't a method on IQueryable
. It's an extension method - a static method which is called as if it were an instance method. So unless NSubstitute can mock out static methods somehow (like Typemock Isolator does), you're going to have problems.
A really smart LINQ-oriented mocking system could essentially provide fake information to Queryable
and respond to similar expression trees when it was given them by the production code - but it would be very fragile. You'd be better off with an in-memory database if at all possible.
In general, you shouldn't rely on your production code making an exact sequence of calls if another sequence would be equivalent. For example, it would be reasonable for your production code to be written as
clients.Where(p=>p.Identifier == "Test").Any()
it's not as readable, but it's fundamentally equivalent to the call to Any
with a predicate. Your test should be able what the production code achieves, not the exact steps it takes to achieve it.