IXRepository and test problems
Posted
by
Ridermansb
on Programmers
See other posts from Programmers
or by Ridermansb
Published on 2013-06-11T01:11:51Z
Indexed on
2013/11/09
22:09 UTC
Read the original article
Hit count: 252
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 implementingIRepositoryalready been made. IfEmployeeRepositoryinherit onlyIEmployeeRepository, I have to literally copy and paste the code ofRepository<T>.In our example, in
EmployeeRepository : Repository<Employee>ourRepositorylies 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
GetInactiveEmployeesreturn all Employees in which the Status != Active and StartDate < DateTime.Now. I can not create a Fake/Mock ofIEmployeeRepositorybecause I would be testing? Need to test the actual implementation ofGetInactiveEmployees.
The complete code can be found on Github
© Programmers or respective owner