【问题标题】:Data driven unit test breaking entity framework connection数据驱动单元测试打破实体框架连接
【发布时间】:2015-09-18 17:56:16
【问题描述】:

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

但是,当我运行测试时,出现无法加载 sqlserver 提供程序的错误:

初始化方法 UnitTest.CalculationTest.MyTestInitialize 抛出 例外。 System.InvalidOperationException: System.InvalidOperationException:实体框架提供程序类型 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' 在应用程序配置文件中注册 对于具有不变名称“System.Data.SqlClient”的 ADO.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);
}

【问题讨论】:

    标签: entity-framework unit-testing data-driven-tests


    【解决方案1】:

    所以我最终设法解决了这个问题。为了将来参考,这里是解决方案:

    Rob Lang 的帖子 Entity Framework upgrade to 6 configuration and nuget magic 让我想起了这里的问题:

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

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

    使用 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);
        }
    

    【讨论】:

      猜你喜欢
      • 2013-10-13
      • 2014-08-10
      • 2023-04-09
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      相关资源
      最近更新 更多