How to mock a generic repository
The short answer is: you don’t. You see, having a mocking library at hand (no matter how cool it is) doesn’t automatically make it the best tool for every testing need.
A generic repository is much easier to replace for testing with a simple fake and allows to use simple state-based testing agaist it, rather than mock verifications.
A fairly typical generic repository might look like the following:
public interface IRepository<T> where T : IEntity
{
T Get(Guid id);
void SaveOrUpdate(T entity);
void Delete(T entity);
IQueryable<T> Query();
}
You might use integer or long for IDs, you might not have an IEntity interface but a base class, you might not have a Query feature there, but that’s beside the point. The point is that such an interface, whatever the variations, is trivial to fake:...
Read full article