如何使用 resharper 和 xunit 2 捕获 NHibernate 的 SQL 语句
How to capture SQL statements of NHibernate with resharper and xunit 2
我正在尝试在单元测试会话的 Resharper 输出中显示来自我的 XUnit 2.x 单元测试的 nhibernate 的 sql 语句,但它确实记录了 sql 语句.
使用 MSpec 测试 没有问题,并且会显示 sql 语句。使用 XUnit 1.x 我认为 sql 语句也被记录了。
我已经通过 属性 show_sql
配置了 NHibernate
<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 语句。
所以有没有人解决这个问题?我试图避免在我的单元测试中集成 log4net,只是为了记录这些语句。
XUnit 2.1,
NHibernate 4.0,
ReSharper 2016.3.1,
Visual Studio 2013
感谢@David Osborne 的评论,我使用 EmptyInterceptor
捕获 NHibernate 的 SQL 语句并将它们写入 XUnit 框架提供的 ITestOutputHelper
。
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())
{
}
}
}
我正在尝试在单元测试会话的 Resharper 输出中显示来自我的 XUnit 2.x 单元测试的 nhibernate 的 sql 语句,但它确实记录了 sql 语句.
使用 MSpec 测试 没有问题,并且会显示 sql 语句。使用 XUnit 1.x 我认为 sql 语句也被记录了。
我已经通过 属性 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 语句。
所以有没有人解决这个问题?我试图避免在我的单元测试中集成 log4net,只是为了记录这些语句。
XUnit 2.1, NHibernate 4.0, ReSharper 2016.3.1, Visual Studio 2013
感谢@David Osborne 的评论,我使用 EmptyInterceptor
捕获 NHibernate 的 SQL 语句并将它们写入 XUnit 框架提供的 ITestOutputHelper
。
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())
{
}
}
}