2017-01-06 25 views
0

我試圖從我的XUnit 2.x unit單元測試單元測試會話的Resharper輸出中顯示nhibernate的sql語句,但它記錄了sql語句。如何使用resharper和xunit捕獲NHibernate的SQL語句2

With MSpec Tests沒有問題,並顯示SQL語句。用XUnit 1.x我想sql語句也被記錄下來。

我已經通過性能配置NHibernate的show_sql

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory> 
    <property name="show_sql">true</property> 
    </session-factory> 
</hibernate-configuration> 

有中的xUnit 2輸出如何獲取a change,但我不知道如何這與NHibernate結合記錄的SQL語句。

那麼有誰有這個解決方案嗎?我試圖在我的unittests中避免log4net集成,只記錄這些語句。

的xUnit 2.1, NHibernate的4.0, ReSharper的2016年3月1日, 的Visual Studio 2013

+0

這裏建議這可能與的xUnit的'ITestOutputHelper'和攔截成爲可能:http://stackoverflow.com/a/2874025/1162077 –

回答

1

感謝@大衛Osborne's發表評論我使用EmptyInterceptor捕捉的NHibernate的SQL語句,並將其寫入到ITestOutputHelper由XUnit框架提供。

public class XUnitSqlCaptureInterceptor : EmptyInterceptor 
{ 
    public XUnitSqlCaptureInterceptor(ITestOutputHelper output) 
    { 
     this.Output = output; 
    } 

    public ITestOutputHelper Output { get; set; } 

    public override SqlString OnPrepareStatement(SqlString sql) 
    { 
     this.Output.WriteLine(sql.ToString()); 

     return sql; 
    } 
} 

的用法如下:

[Fact] 
public void MyUnitTestWithSQL() 
{ 
    using (var session = factory.OpenSession(new XUnitSqlCaptureInterceptor(this.output))) 
    { 
     using (var transcation = session.BeginTransaction()) 
     { 

     } 
    } 
}