简体   繁体   English

模拟实体框架存储库模式

[英]Mocking Entity Framework repository pattern

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. 我想对查询进行单元测试,以确保我具有正确的where子句,并且在结果中包括一个额外的实体。

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. 我一直在研究如何使用Moq模拟DbContext和DbSet,但是看不到如何仍然具有包含的EF功能。 Most of the examples I've found are mocking out a simple GetById. 我发现的大多数示例都在模拟一个简单的GetById。 Basically I don't want to mock out EF, just get it to read from in memory rather than from a Db. 基本上,我不想模拟EF,只需从内存而不是从Db中读取它即可。

Any ideas? 有任何想法吗?

Thanks 谢谢

After looking into this some more I've realised what I want to do is not possible. 在进一步研究之后,我意识到我想做的事是不可能的。

What I wanted was to mock out the DB with in-memory storage and then test that my queries work including the Include method (eg have some tests that include related entities and some that don't). 我想要的是在内存中模拟数据库,然后测试包括Include方法在内的查询是否有效(例如,有些测试包含相关的实体,而有些则没有)。 I didn't want to mock out Include I actually wanted it to act as implemented against my in-memory list. 我不想模拟“ Include但实际上我希望它对我的内存列表实施。 This is not possible as from the following here : 这是不可能的,因为从以下内容开始

One example of such a difference is loading related data. 这种差异的一个例子是加载相关数据。 If you create a series of Blogs that each have related Posts, then when using in-memory data the related Posts will always be loaded for each Blog . 如果您创建了一系列每个都有相关帖子的Blog ,那么在使用内存数据时,始终会为每个Blog加载相关帖子 However, when running against a database the data will only be loaded if you use the Include method. 但是,在针对数据库运行时,仅在使用Include方法时才会加载数据。

For this reason, it is recommended to always include some level of end-to-end testing (in addition to your unit tests) to ensure your application works correctly against a database. 因此,建议始终包括某种级别的端到端测试(除了单元测试之外),以确保您的应用程序可以正确地针对数据库运行。

There is a tool called Effort which is good for Entity Framework unit testing. 有一个名为Effort的工具,非常适合实体框架单元测试。 Might be worth a look to see if it fits with your requirements? 可能值得一看,看看它是否符合您的要求?

From their home page: 从他们的主页:

It is basically an ADO.NET provider that executes all the data operations on a lightweight in-process main memory database instead of a traditional external database 它基本上是一个ADO.NET提供程序,它在轻量级进程内主内存数据库而不是传统的外部数据库上执行所有数据操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM