This is a little artificial for a unit test, since each LINQ provider is implementation specific. In your test, you can use various approaches, and it just will not tell you anything about the real implementation. For example, if he mocks through LINQ-to-Objects, I can use:
List<Animal> = myAnimalRepository.Find(x => CheckSpecies(x, "Cat")); ... static bool CheckSpecies(Animal animal, string species) { return animal.Species == species; }
This will work with LINQ-to-Objects ... but only with LINQ-to-Objects. Similarly, using UDF (or one of the SQL helper methods) will work in LINQ-to-SQL, but not in the Entity Framework.
I came to the conclusion that only integration tests are useful for this scenario, so no; mockery is not very useful. This will give you a warm, happy feeling that you have done something useful, but in the end it does not verify that what you write in your application will work.
An even better approach, IMO, is not to expose such an interface through your repository interface. If you limit LINQ queries to your data layer, you eliminate the risk, and now you are testing (/ mocking) a clean, predicate interface.
I discuss this one here .
source share