数据驱动单元测试打破实体框架连接



我有一个使用实体框架的应用程序。我正在编写一个单元测试,我想从CSV文件中使用数据驱动测试。

但是,当我运行测试时,我得到一个错误,sqlserver提供程序无法加载:

初始化方法UnitTest.CalculationTest.MyTestInitialize抛出例外。系统。InvalidOperationException:系统。InvalidOperationException:实体框架提供程序类型"System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework。在应用程序配置文件中注册的SqlServer'为ADO。具有不变名称"System.Data.SqlClient"的。NET提供程序无法加载。确保程序集限定名称为已使用,并且程序集对正在运行的应用程序可用。
  1. 如果我删除数据驱动的方面,只测试一个值,那么测试工作。
  2. 如果我只是使用数据驱动的方面,并删除实体框架的东西,那么测试工作。

所以,只有当我尝试同时使用实体框架活动的数据驱动测试时,我才会得到错误。那么,我哪里做错了呢?

这是我的测试方法:

[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的帖子,实体框架升级到6配置和nuget魔法,提醒了我这里的问题:

类中引用的DLL无法加载类型时,

项目,这通常意味着它没有被复制到输出中bin/目录中。当您不使用引用的类型时

当您在测试中使用部署项时,这将引起它的丑陋的头。如果您在测试中使用了一个部署项,那么所有的所需的二进制文件都被复制到部署目录中。问题是,如果您使用的是动态加载的项目,那么测试套件并不知道它必须复制那些项目。

使用实体框架,这意味着您的提供者将不会被复制到部署位置,并且您将根据我的问题收到错误。

要解决此问题,只需确保您的实体框架提供程序也被标记为部署项。

因此,请注意在我的测试属性中包含 deploymenttem(@"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);
    }

最新更新