Entity framework 数据驱动单元测试破坏实体框架连接
我有一个使用实体框架的应用程序。我正在编写一个单元测试,我想使用CSV文件中的数据驱动测试 但是,当我运行测试时,出现一个错误,无法加载sqlserver提供程序: 初始化方法UnitTest.CalculationTest.MyTestInitialize 例外。System.InvalidOperationException: System.InvalidOperationException:实体框架提供程序类型 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer已在应用程序配置文件中注册 对于具有固定名称“System.Data.SqlClient”的ADO.NET提供程序 无法加载。确保程序集限定名为 已使用,并且程序集对正在运行的应用程序可用Entity framework 数据驱动单元测试破坏实体框架连接,entity-framework,unit-testing,data-driven-tests,Entity Framework,Unit Testing,Data Driven Tests,我有一个使用实体框架的应用程序。我正在编写一个单元测试,我想使用CSV文件中的数据驱动测试 但是,当我运行测试时,出现一个错误,无法加载sqlserver提供程序: 初始化方法UnitTest.CalculationTest.MyTestInitialize 例外。System.InvalidOperationException: System.InvalidOperationException:实体框架提供程序类型 'System.Data.Entity.SqlServer.SqlProvid
[TestMethod, TestCategory("Calculations")
, DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV"
, "ConvertedMeanProfileDepth.csv", "ConvertedMeanProfileDepth#csv"
, Microsoft.VisualStudio.TestTools.UnitTesting.DataAccessMethod.Sequential)
, DeploymentItem("ConvertedMeanProfileDepth.csv")]
public void ConvertedMeanProfileDepthTest()
{
ConvertedMeanProfileDepth target = new ConvertedMeanProfileDepth();
Decimal mpd = decimal.Parse(this.TestContext.DataRow["mpd"].ToString());
Decimal expected = decimal.Parse(this.TestContext.DataRow["converted"].ToString());
Decimal actual;
actual = target.Calculate(mpd);
Assert.AreEqual(expected, actual);
}
所以我最终设法解决了这个问题。以下是解决方案供将来参考: Rob Lang的帖子提醒了我这里的问题: 无法为在中引用的DLL加载类型时 项目,通常意味着尚未将其复制到输出 bin/目录。当您不使用引用的 库中,它将不会被复制 当您在测试中使用部署项时,这将使其丑陋不堪。如果在测试中使用部署项,则所有必需的二进制文件都会复制到部署目录。问题是,如果您使用的是动态加载的项,那么测试套件不知道必须复制这些项 对于Entity Framework,这意味着您的提供程序将不会复制到部署位置,并且您将收到我的问题所述的错误 要解决此问题,只需确保实体框架提供程序也标记为部署项 因此,请注意在我的测试属性中包含了部署项(@“EntityFramework.SqlServer.dll”)。从这里开始,一切都非常完美:
[TestMethod, TestCategory("Calculations")
, DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV"
, "ConvertedMeanProfileDepth.csv", "ConvertedMeanProfileDepth#csv"
, Microsoft.VisualStudio.TestTools.UnitTesting.DataAccessMethod.Sequential)
, DeploymentItem("ConvertedMeanProfileDepth.csv")
, DeploymentItem(@"EntityFramework.SqlServer.dll")]
public void ConvertedMeanProfileDepthTest()
{
ConvertedMeanProfileDepth target = new ConvertedMeanProfileDepth();
Decimal mpd = decimal.Parse(this.TestContext.DataRow["mpd"].ToString());
Decimal expected = decimal.Parse(this.TestContext.DataRow["converted"].ToString());
Decimal actual;
actual = target.Calculate(mpd);
Assert.AreEqual(expected, actual);
}