2015-01-15 39 views
1

我面臨UnitTest的問題。我想測試使用基於NPoco的存儲庫完成的數據訪問。因此,我寫了一些測試,測試項目通過NuGet檢索NUnit,NPoco,System.Data.SQLite和其他一些東西。當使用NUnit執行測試時未找到System.Data.SqLite DataProvider

這是TestProject的的app.config:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <connectionStrings> 
     <add name="RepositoryTests.Properties.Settings.ConnectionString" connectionString="Data Source=db.sqlite;Version=3" /> 
    </connectionStrings> 
    <system.data> 
     <DbProviderFactories> 
      <remove invariant="System.Data.SQLite"/> 
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /> 
     </DbProviderFactories> 
    </system.data> 
</configuration> 

在VS,項目構建的罰款。在Visual Studio中觸發測試也可以。

使用MSBUILD構建項目也很有用。與msbuild.exe他們建設運行後通過NUnit的測試提出了一個例外,但:

​​

這只是個案,執行直接使用NUnit測試時(某事像nunit-console.exe myproject.csproj /config:Release)。在VS中觸發它們不成問題。

有誰知道如何解決這個問題?

回答

1

該問題是由具有其自己的app.config的nunit測試運行器導致的,其中您的設置不存在。

解決問題的最簡單方法是將配置移出app.config並放入代碼本身。無論你在app.config中有什麼,都可以在代碼中完成。

另一種解決方案是將配置移動到單獨的文件中,然後使用代碼顯式加載該文件的配置。只要確保該文件被複制到構建的輸出文件夾。

您可以使用類似:

public static class TestFactory 
{ 
    public static DatabaseFactory DbFactory { get; set; } 

    public static void Setup() 
    { 
     var fluentConfig = FluentMappingConfiguration.Configure(new OurMappings()); 
     //or individual mappings 
     //var fluentConfig = FluentMappingConfiguration.Configure(new UserMapping(), ....); 

     DbFactory = DatabaseFactory.Config(x => 
     { 
      // Load the connection string here or just use a constant in code... 
      x.UsingDatabase(() => new Database("connString"); 
      x.WithFluentConfig(fluentConfig); 
      x.WithMapper(new Mapper()); 
     }); 
    } 
} 

詳情請參閱here

然後在測試夾具:

[TestFixture] 
public class DbTestFixture 
{ 
    [TestFixtureSetUp] 
    public void Init() 
    { 
     TestFactory.Setup(); 
    } 

    [Test] 
    public void YourTestHere() 
    { 
     var database = TestFactory.DbFactory.GetDatabase(); 
     ... 
    } 
} 
+0

感謝您的回答,但不幸的是您提供的代碼沒有辦法。但我會對它進行投票,因爲帶有nunit-config-file-problem的提示最終會導致(hacky)解決方案。 – nozzleman

0

我把它通過創建app.config -file的複製工作,並給該副本測試項目的名稱,然後.config。因此,如果我們假設,該項目名爲Test.Project副本必須命名爲Test.Project.config。 Nunit似乎沒有加載自動生成的Test.Project.dll.config。這個信息可以在NUnit-docs(配置文件 - >測試配置文件 - >第3段,最後一句)中找到。

在VS中,在複製文件的屬性部分中,我將copy-to-output-directory-property設置爲always

執行與nunit-console.exe測試繼而導致另一異常(Bad-Image),其通過引起的NUnit沒有找到SQLite.Interop.dll -file。這可以通過添加已存在於x64x86文件夾中的文件作爲VS中現有的解決方案元素並將copy-to-output-dir-property設置爲always來解決。

相關問題