使用Entity Framework的测试无法使用“全部运行”,但是在使用“运行选定的测试”时可以成功

时间:2019-10-11 15:21:56

标签: c# entity-framework entity-framework-6 mstest

我的解决方案中有很多测试。测试分布在几个不同的测试项目中。我将Entity Framework 6.3与基于代码的配置一起使用。我的DbConfiguration类看起来像这样

namespace MySolution.Data
{
    public class MyEntityConfiguration : DbConfiguration
    {
        public MyEntityConfiguration()
        {
            AddInterceptor(new StringTrimmerInterceptor());
        }
    }

    public class StringTrimmerInterceptor : IDbCommandTreeInterceptor
    {
        // ... do stuff
    }
}

当单击测试的“全部运行”按钮时,使用我的Entity Framework上下文的测试将失败,并显示此错误。

  

System.InvalidOperationException: The default DbConfiguration instance was used by the Entity Framework before the 'MyEntityConfiguration' type was discovered. An instance of 'MyEntityConfiguration' must be set at application start before using any Entity Framework features or must be registered in the application's config file.

但是,如果我突出显示所有测试并单击“运行选定的测试”,则所有测试都会成功。

如果我编辑测试项目的app.config并在codeConfigurationType="MySolution.Data.MyEntityConfiguration, MySolution.Data"行上添加<entityFramework>,则单击Run All,可以使测试成功。

我的问题是:

  • 为什么必须将codeConfigurationType放入我的app.config文件中才能使其工作?实体框架6.3是否不应该允许我通过DbConfiguration来做到这一点?在我运行应用程序时,使用DbConfiguration进行的配置可以正常运行,但在测试过程中似乎无法正常工作。
  • 为什么通过单击“全部运行”运行所有测试时我的测试失败了,但是如果我通过突出显示所有测试并单击“运行选定的测试”来运行所有测试,则相同的测试成功了吗?

0 个答案:

没有答案