score:3

Accepted answer

If Find<T>() is an extension method on IRepository, then I don't think it'll work, because you're dealing with a static type.

If Find<T>() is a method on IRepository, then you set up a return value only for repository.Find<Person>().

Create an IEnumerable<> or IQueryable<> for Find<>() to return (whichever type it is actually supposed to return -- I don't know which it is), and the LINQ expression will just do its work on that in the actual class. You don't have to mock the LINQ because that is just filtering the results of your own method, even though in production, if this is, say, Entity Framework or LINQ to SQL, the whole expression will instead be converted to an optimized database call.

If in your setup you give the collection an item with a matching ID, you'll assert that you get it back.
If in your setup you don't give the collection an item with a matching ID, you'll assert that you get null (the default).
If in your setup you return null from repository.Find<>(), you'll assert than an exception is thrown because you can't call the extension methods on null.


Related Articles