简体   繁体   English

使用Effort.EF6测试预加载方案

[英]Testing eager-loading scenarios with Effort.EF6

We're using Effort.EF6 to build a test suite for an ASP.NET Web API 2 service working against an in-memory database, and for the most part it's a wonderful experience. 我们正在使用Effort.EF6来构建针对内存数据库的ASP.NET Web API 2服务的测试套件,并且在大多数情况下它是一种非常棒的体验。

However, for reasons unrelated to this question, we have had to turn off lazy loading in EF6 (by running Configuration.LazyLoadingEnabled = false; in the constructor of the db context), which means that if we forget to .Include() a relation somewhere and later use it, we get NullReferenceExceptions . 但是,由于与此问题无关的原因,我们不得不关闭EF6中的延迟加载(通过运行Configuration.LazyLoadingEnabled = false;在db上下文的构造函数中),这意味着如果我们忘记.Include()关系某处,后来使用它,我们得到NullReferenceExceptions We'd like our tests to catch these types of errors. 我们希望我们的测试可以捕获这些类型的错误。

Our test setup is basically as follows: 我们的测试设置基本如下:

  1. Create a db connection using Effort's connection factories. 使用Effort的连接工厂创建数据库连接。 This is a unique connection instance (with a unique key, when we try with persistent connections) per test. 每次测试时,这是一个唯一的连接实例(当我们尝试使用持久连接时使用唯一键)。
  2. Create a db context with that connection, and add the data we want to exist for the test. 使用该连接创建db上下文,并添加我们想要存在的数据以进行测试。
  3. Register a service override in the DI container for the db context. 在DI容器中为db上下文注册服务覆盖。

The problem is we can't figure out how to configure Effort.EF6 to disallow lazy loading, since it seems that anything we've added to the context in our test setup is already loaded when the code under test runs, and thus we never see the exceptions. 问题是我们无法弄清楚如何配置Effort.EF6以禁止延迟加载,因为我们在测试设置中添加到上下文中的任何内容似乎已经在测试中的代码运行时加载,因此我们从不看例外。 We're assuming this is because we're re-using the context instance between test setup and actual code execution. 我们假设这是因为我们在测试设置和实际代码执行之间重用了上下文实例。

If we try to switch the third step to the following: 如果我们尝试将第三步切换到以下步骤:

  1. Register a service override in the DI container for the db connection. 在DI容器中为数据库连接注册服务覆盖。
  2. The DI container creates a db context instance (taking the db connection as an injected dependency) DI容器创建一个db上下文实例(将db连接作为注入依赖项)

we instead end up with an empty db context, ie it has no data despite us adding a few data points before running the test. 我们最终得到一个空的 db上下文,即尽管我们在运行测试之前添加了一些数据点,但它没有数据。

How can we construct and inject a db context using Effort, which will fail if the .Include() statement is missing but work if it's there? 我们如何使用Effort构造和注入db上下文,如果缺少.Include()语句,它将失败但是如果它在那里会工作?

You have to deactivate it in the constructor for Effort.EF6 too - the one that takes the DbConnection object. 您必须在Effort.EF6的构造函数中停用它 - 获取DbConnection对象的构造函数。

I solved it for my EF Code First project like followed: 我解决了我的EF Code First项目,如下所示:

public class MyDataContext : DbContext, IMyDataContext
{
    /*
    * the ohter stuff like IDbSet<T> etc.
    */


    public MyDataContext() : base("name=MyConnectionString")
    {
        Configure();
    }

    /// <summary>
    /// This constructor is for test usage only!
    /// </summary>
    /// <param name="connection">Test connection for in memory database</param>
    public MyDataContext(DbConnection connection) : base(connection, true)
    {
        Configure();
    }


    /// <summary>
    /// Configures the data context.
    /// </summary>
    private void Configure()
    {
        Configuration.LazyLoadingEnabled = false;
    }
}

Furthermore in my test projects I am setting up test CSV files. 此外,在我的测试项目中,我正在设置测试CSV文件。 The files will be used by Effort.EF6 to instantiate a context with seeded data. Effort.EF6将使用这些文件来实例化具有种子数据的上下文。 In my case I wrote a small TestDataManager that gives the possability to seed specific tables with specific files. 在我的例子中,我写了一个小的TestDataManager,它可以为特定的文件提供特定的表。

Here is a piece of code how you could seed data with Effort.EF6: 以下是一段代码,您可以使用Effort.EF6对数据进行种子设定:

IDataLoader loader = new CsvDataLoader(@"C:\Temp\Test_CSV_Files");
DbConnection dbConnection = DbConnectionFactory.CreateTransient(loader);
var myDataContext = new MyDataContext(dbConnection);

Maybe this blog post could help you too. 也许这篇博文也可以帮到你。

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

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