数据驱动单元测试中断 entity framework 连接
Data driven unit test breaking entity framework connection
我有一个使用 entity framework 的应用程序。我正在编写一个单元测试,我想在其中使用来自 CSV 文件的数据驱动测试。
但是,当我 运行 测试时,我收到无法加载 sqlserver 提供程序的错误:
Initialization method UnitTest.CalculationTest.MyTestInitialize threw
exception. System.InvalidOperationException:
System.InvalidOperationException: The Entity Framework provider type
'System.Data.Entity.SqlServer.SqlProviderServices,
EntityFramework.SqlServer' registered in the application config file
for the ADO.NET provider with invariant name 'System.Data.SqlClient'
could not be loaded. Make sure that the assembly-qualified name is
used and that the assembly is available to the running application.
- 如果我删除数据驱动方面并仅测试单个值,则测试有效。
- 如果我只使用数据驱动方面并删除 Entity Framework 东西,那么测试就可以了。
所以,只有当我尝试在 entity framework 处于活动状态的情况下使用数据驱动测试时,我才会收到错误消息。那么,我哪里出错了?
这是我的测试方法:
[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 的 post、Entity Framework upgrade to 6 configuration and nuget magic 让我想起了这里的问题:
When a type cannot be loaded for a DLL that is referenced in a
project, it usually means that it has not been copied to the output
bin/ directory. When you're not using a type from a referenced
library, it will not be copied.
当您在测试中使用部署项时,这会引起它丑陋的反应。如果您在测试中使用部署项,那么所有 required 二进制文件都将复制到部署目录。问题是,如果您使用动态加载的项目,那么测试套件不知道它必须复制这些项目。
使用 Entity Framework,这意味着您的提供程序不会被复制到部署位置,您将收到我的问题中的错误消息。
要解决此问题,只需确保您的 entity framework 提供商也被标记为部署项。
因此,请注意我的测试属性中包含 DeploymentItem(@"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);
}
我有一个使用 entity framework 的应用程序。我正在编写一个单元测试,我想在其中使用来自 CSV 文件的数据驱动测试。
但是,当我 运行 测试时,我收到无法加载 sqlserver 提供程序的错误:
Initialization method UnitTest.CalculationTest.MyTestInitialize threw exception. System.InvalidOperationException: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application.
- 如果我删除数据驱动方面并仅测试单个值,则测试有效。
- 如果我只使用数据驱动方面并删除 Entity Framework 东西,那么测试就可以了。
所以,只有当我尝试在 entity framework 处于活动状态的情况下使用数据驱动测试时,我才会收到错误消息。那么,我哪里出错了?
这是我的测试方法:
[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 的 post、Entity Framework upgrade to 6 configuration and nuget magic 让我想起了这里的问题:
When a type cannot be loaded for a DLL that is referenced in a project, it usually means that it has not been copied to the output bin/ directory. When you're not using a type from a referenced library, it will not be copied.
当您在测试中使用部署项时,这会引起它丑陋的反应。如果您在测试中使用部署项,那么所有 required 二进制文件都将复制到部署目录。问题是,如果您使用动态加载的项目,那么测试套件不知道它必须复制这些项目。
使用 Entity Framework,这意味着您的提供程序不会被复制到部署位置,您将收到我的问题中的错误消息。
要解决此问题,只需确保您的 entity framework 提供商也被标记为部署项。
因此,请注意我的测试属性中包含 DeploymentItem(@"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);
}