Reputation: 867
A have an application with a typical scenario data access layer (DAL):
To give some context, here are some classes/interfaces.
Generic repository interface:
public interface IRepository<T> : IDisposable where T : class
{
void Add(T entity);
void Update(T entity);
void Obliterate(T entity);
void Obliterate(Expression<Func<T, bool>> where);
T GetById(long id);
T GetById(string id);
IQueryable<T> GetAll();
IQueryable<T> GetMany(Expression<Func<T, bool>> where);
T GetSingle(Expression<Func<T, bool>> where);
void SaveChanges();
}
Repository base:
public abstract class RepositoryBase<T> : IRepository<T> where T : class
{
...
}
A repository for the Foo entity:
public class FooRepository : RepositoryBase<File>, IFooRepository
{
// Specific methods here
...
}
How should I test the repositories? Right now I have a test class for each repository, with test methods that are very similar in all of them, as they are mostly testing generic methods from the RepositoryBase. It's obvious that I need tests for the specific methods, but for the global generic ones should I keep testing them against each different entity? I don't know if it's wise to assume that if insertion, for instance, works for Foo entities it will also work for others; however testing for each has an added overhead in terms of test creation and maintenance. Can you recommend any best practice around this?
(By the way, these are integration tests)
Thanks
Upvotes: 2
Views: 92
Reputation: 236208
I don't know if it's wise to assume that if insertion, for instance, works for Foo entities it will also work for others
No, you can't assume this. What if some entity do not have correct mapping? What if you forgot to define DbSet<Bar>
on your DbContext
? If you want to be completely sure, you should test all methods of all concrete repositories.
however testing for each has an added overhead in terms of test creation and maintenance
Correct. That's why instead of writing integration tests for repositories only, write acceptance tests for your application. You will exercise whole stack and concrete repositories will be involved.
Upvotes: 2