2011-06-04 64 views
6

當我用流利的NHibernate的運行內存使用SQLite數據庫1.0.66.0測試(MS測試):VS MSTest的轉輪鎖System.Data.SQLite.dll在內存中運行測試

[TestClass] 
public abstract class InMemoryDatabaseTest 
{ 
    private NHibernate.Cfg.Configuration configuration; 
    private ISessionFactory sessionFactory; 

    [TestInitialize] 
    public void Initialize() 
    { 
     // All "CreateConfiguration" does is load FNh mappings. 
     this.configuration = new NhConfigurationBuilder() 
      .CreateConfiguration() 
      .Database(() => SQLiteConfiguration.Standard.InMemory()) 
      .BuildConfiguration(); 

     this.sessionFactory = this.configuration.BuildSessionFactory(); 
    } 

    [TestCleanup] 
    public void Cleanup() 
    { 
     new SchemaExport(this.configuration).Drop(false, true); 
     sessionFactory.Dispose(); 
    } 

    protected ISession CreateSession() 
    { 
     var session = this.sessionFactory.OpenSession(); 

     // Re-create the database every time a new session is created. 
     new SchemaExport(this.configuration) 
      .Execute(script: false, export: true, justDrop: false, connection: session.Connection, exportOutput: null); 

     session.BeginTransaction(); 
     return session; 
    } 
} 

而且然後用這個作爲一個例子:

[TestClass] 
public class MessagesControllerTests : InMemoryDatabaseTest 
{ 
    [TestMethod] 
    public void SQLite_should_have_all_handles_released() 
    { 
     using (var session = this.CreateSession()) 
     { 
      // Don't even need to do anything. 
     } 
    } 
} 

運行這個測試之後,我嘗試Clean整體解決方案。結果如下:

  • 運行此測試(CTRL + R,CTRL + T)時,clean將能夠按預期成功。
  • 在調試這個測試中(CTRL + R,T),清潔失敗出現錯誤:C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(3607,9): warning MSB3061: Unable to delete file "PathToProject\bin\Debug\System.Data.SQLite.DLL". Access to the path 'PathToProject\bin\Debug\System.Data.SQLite.DLL' is denied.

我首先想到的是確定,刪除DLL。當我嘗試時,系統會提示QTAgent32.exe正在使用該DLL。我使用Process Explorer來驗證這一點。出於某種原因,ms測試運行器正在保持DLL的句柄。我試着從another question修改Cleanup metehod有一些建議,但它仍然沒有奏效:

[TestCleanup] 
public void Cleanup() 
{ 
    new SchemaExport(this.configuration).Drop(false, true); 
    sessionFactory.Close(); 
    sessionFactory.Dispose(); 
    SQLiteConnection.ClearAllPools(); 
    GC.Collect(); 
} 

我已經能夠重現這在3噸不同的機器。任何已知的方法來解決這個問題將不勝感激。

更新:我已經清理了一些語言混亂。實際的解決方案配置可以在Debug/Relase中。但是,運行測試與調試測試會導致錯誤消息的差異。

+0

爲什麼要清理? – 2011-06-04 07:01:16

+0

嗯,只是爲了確保數據庫被完全銷燬。 – TheCloudlessSky 2011-06-04 10:00:14

+0

奇怪的是,即使您構建在發佈模式下,錯誤消息中的路徑仍然是bin \ Debug – 2011-06-22 23:22:11

回答

0

我一直有一個類似的問題(SQLite.dll被Visual Studio測試運行程序鎖定,儘管在調試模式下主要使用MbUnit測試),並發現使用TestDriven.net運行測試解決了我的問題。在此之後,我從來沒有進一步看過MSTest亞軍,對不起。

相關問題