IXRepository and test problems
- by Ridermansb
Recently had a doubt about how and where to test repository methods.
Let the following situation: I have an interface IRepository like this:
public interface IRepository<T>
where T: class, IEntity
{
IQueryable<T> Query(Expression<Func<T, bool>> expression);
// ... Omitted
}
And a generic implementation of IRepository
public class Repository<T> : IRepository<T>
where T : class, IEntity
{
public IQueryable<T> Query(Expression<Func<T, bool>> expression)
{
return All().Where(expression).AsQueryable();
}
}
This is an implementation base that can be used by any repository. It contains the basic implementation of my ORM.
Some repositories have specific filters, in which case we will IEmployeeRepository with a specific filter:
public interface IEmployeeRepository : IRepository<Employee>
{
IQueryable<Employee> GetInactiveEmployees();
}
And the implementation of IEmployeeRepository:
public class EmployeeRepository : Repository<Employee>, IEmployeeRepository // TODO: I have a dependency with ORM at this point in Repository<Employee>. How to solve? How to test the GetInactiveEmployees method
{
public IQueryable<Employee> GetInactiveEmployees()
{
return Query(p => p.Status != StatusEmployeeEnum.Active || p.StartDate < DateTime.Now);
}
}
Questions
Is right to inherit Repository<Employee>?
The goal is to reuse code once all implementing IRepository already been made. If EmployeeRepository inherit only IEmployeeRepository, I have to literally copy and paste the code of Repository<T>.
In our example, in EmployeeRepository : Repository<Employee> our Repository lies in our ORM layer. We have a dependency here with our ORM impossible to perform some unit test.
How to create a unit test to ensure that the filter GetInactiveEmployees return all Employees in which the Status != Active and StartDate < DateTime.Now. I can not create a Fake/Mock of IEmployeeRepository because I would be testing? Need to test the actual implementation of GetInactiveEmployees.
The complete code can be found on Github