I have an interface defined as:
public interface IRepository<TEntity> where TEntity : BaseEntity
{
...
IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "");
...
}
And my implementation as:
public class Repository<TEntity> : IRepository<TEntity> where TEntity : BaseEntity
{
internal MyContext context;
internal DbSet<TEntity> dbSet;
public Repository(MyContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
}
And finally my code that calls this:
Repository.Get(r =>
r.SourceOrganisationId == id,
null, // No ordering
"DestinationOrganisation") // Include the company
.Select(d => d.DestinationOrganisation).OrderBy(c => c.Name);
I want to unit test my query, to make sure that I've got the correct where clause, and I'm including an extra entity in the results.
I've been looking at how to mock out the DbContext and DbSet using Moq, but can't see how to still have the EF functionality of the includes. Most of the examples I've found are mocking out a simple GetById. Basically I don't want to mock out EF, just get it to read from in memory rather than from a Db.
Any ideas?
Thanks