单位测试与努力和SQL CE并行失败 [英] Unit testing with Effort and SQL CE in parallel fails

查看:174
本文介绍了单位测试与努力和SQL CE并行失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在评估使用EF6的单元测试结合

I'm evaluating unit tests using EF6 in combination with

http:// www。 codeproject.com/Articles/460175/Two-strategies-for-testing-Entity-Framework-Effort 是一个很好的参考,但现在我被困住了。

http://www.codeproject.com/Articles/460175/Two-strategies-for-testing-Entity-Framework-Effort was a quite good reference but now I'm stuck.

我有2个测试项目(一个用于努力,另一个用于SQL CE)。如果我分别运行一切都好。一直运行在ReSharper测试运行器的最后一个测试项目总是失败。

I have 2 test projects (one for Effort and the other for SQL CE). If I'm running both separately everthing's fine. Running both in a row with the ReSharper test runner the last test project always fails. Either


System.InvalidOperationException:在尝试添加$ b之前,使用DbConfiguration实例,实体框架已经是
$ b'加载'事件处理程序。 加载事件处理程序只能在使用实体框架之前启动应用程序的
部分添加。

System.InvalidOperationException : The Entity Framework was already using a DbConfiguration instance before an attempt was made to add an 'Loaded' event handler. 'Loaded' event handlers can only be added as part of application start up before the Entity Framework is used.


System.InvalidOperationException:在尝试设置$ b之前,实体框架使用了默认的DbConfiguration实例
$ S实例的SqlCeConfiguration。在使用任何实体框架功能
之前,必须在应用程序启动时设置$ s $ c $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b

System.InvalidOperationException: The default DbConfiguration instance was used by the Entity Framework before an attempt was made to set an instance of 'SqlCeConfiguration'.The 'SqlCeConfiguration' instance must be set at application start before using any Entity Framework features or must be registered in the application's config file.

它总是一样的。后继继承者从前身继承DbConfiguration实例。

It's always the same. The successor inherits the DbConfiguration instance from the predecessor. How can I run both test projects / configuration without side effects?

这是我的 DbContext 类:

public class DataContext : DbContext
{
    public DataContext(string connectionString) : base(connectionString)
    { Configuration.LazyLoadingEnabled = false; }

    public DataContext(DbConnection connection) : base(connection, true)
    { Configuration.LazyLoadingEnabled = false; }
}

这是具有努力的测试夹具: / p>

That's the test fixture with Effort:

[TestFixtureSetUp]
public void TestFixtureSetup()
{
    EffortProviderConfiguration.RegisterProvider();
    var connection = DbConnectionFactory.CreateTransient();
    var dbContext = new DataContext(connection);
    ...
}

这是具有 SQL的测试夹具CE

That's the test fixture with SQL CE:

[TestFixtureSetUp]
public void TestFixtureSetup()
{
    const string filePath = @"LocalDb.sdf";
    var connectionString = string.Format("Data Source={0}; Persist Security Info=False;", filePath);
    DbConfiguration.SetConfiguration(new SqlCeConfiguration());

    var dbContext = new DataContext(connectionString);
    dbContext.Database.Create();
    ...
}

和我的 SqlCeConfiguration

and the my SqlCeConfiguration:

public class SqlCeConfiguration : DbConfiguration
{
    public SqlCeConfiguration()
    {
        SetProviderServices(SqlCeProviderServices.ProviderInvariantName, SqlCeProviderServices.Instance);
        SetDefaultConnectionFactory(new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"));
    }
}

非常感谢!

Marcel

推荐答案

我们在构建服务器上运行所有UnitTests时看到相同的错误,在本地运行所有UnitTests时。

We were seeing the same errors when running all UnitTests on our build server or when running all UnitTests locally.


System.InvalidOperationException:实体框架在尝试之前已使用DbConfiguration实例
添加一个
'Loaded'事件处理程序。 加载事件处理程序只能在使用实体框架之前启动应用程序
添加。

System.InvalidOperationException : The Entity Framework was already using a DbConfiguration instance before an attempt was made to add an 'Loaded' event handler. 'Loaded' event handlers can only be added as part of application start up before the Entity Framework is used.

一次我们将Effort Provider注册码从[TestInitialize]方法移动到AssemblyInitialize方法,一切都开始工作。根据报告错误中的消息,似乎注册不能多次发生。

Once we moved the Effort Provider registration code from the [TestInitialize] method to the AssemblyInitialize method, everything started working. Based on the message in the reported error, it appears that the registration cannot happen more than once.

    [AssemblyInitialize()]
    public static void AssemblyInit(TestContext context)
    {
        Effort.Provider.EffortProviderConfiguration.RegisterProvider();
    }

这篇关于单位测试与努力和SQL CE并行失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆