2015-09-18 28 views
0

我有一個使用實體框架的應用程序。我正在編寫一個單元測試,我想從CSV文件中使用數據驅動的測試。數據驅動的單元測試打破實體框架連接

然而,當我運行測試,我得到一個錯誤的SQLSERVER提供商無法加載:

初始化方法UnitTest.CalculationTest.MyTestInitialize拋出 例外。 System.InvalidOperationException: System.InvalidOperationException:實體框架提供程序類型 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'在應用程序配置文件 中爲具有不變名稱System的ADO.NET提供程序註冊。無法加載Data.SqlClient' 。確保使用裝配限定名稱 ,並且裝配對於運行的應用程序可用。

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

回答

2

所以我設法解決它到底。對於未來的參考,這裏的解決方案:

羅布郎的帖子,Entity Framework upgrade to 6 configuration and nuget magic,提醒我這裏的問題:

當一個類型不能加載是在 項目中引用,它通常是一個DLL意味着它沒有被複制到輸出 bin /目錄。當您不使用引用的 庫中的類型時,它不會被複制。

而這會在您測試中使用部署項目時增加其醜陋的頭部。如果您在測試中使用部署項目,則所有必需的二進制文件都將被複制到部署目錄。問題是,如果您正在使用動態加載的項目,則測試套件不知道它必須複製這些項目。

使用實體框架,這意味着您的提供者將不會被複制到部署位置,並且您將收到錯誤按照我的問題。

要解決該問題,只需確保您的實體框架提供程序也標記爲部署項目。

因此,請注意在我的測試屬性中包含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); 
    }