views:

114

answers:

3

I am using Linq to NHibernate.

I have a following test case :

[TestMethod]
[DeploymentItem("hibernate.cfg.xml")]
public void Category_Should_GetAllByLinq()
{
  // Arrange
  IRepository<Category> repo = new CategoryRepository();

  //Act
  IList<Category> list = repo.GetListByLinq();

  //Assert
  Assert.IsTrue(list.Count > 0);
}

and also I have following code in CategoryRepository class :

public IList<Category> GetListByLinq()
{
  ISession session = NHibernateHelper.OpenSession();

  // When following statement is executed, How to check converted real sql query?
  var query = from c in session.Linq<Category>()
                        select c;

  return query.ToList();
}

My question is that once linq to nhibernate statement is executed, How to check real converted sql query? any method?

I know I can use SQL profiler, However I'd like to use Mock object in test case so I don't want see it in any Database related method.

A: 

Have you tried NHiberate Profiler? It should be what you are looking for.

Rohit Agarwal
NhProf is ok. But not for this purpose. The question was how you can get the generated SQL in a unit test and assert on it.
Dmytrii Nagirniak
+1  A: 

Good question.

I think the best way to assert the SQL generated would be to use IInterceptor and hook into OnPrepareStatement.

Or if it is ok for you to just see the SQL statements in the Output window during debugging you can just enable show_sql option.

Dmytrii Nagirniak
thanks Dmitriy, So there is no way to check generated sql query in tracing debug time, right? you mean It is possible only when to use interceptor or onprparestatement, right?
kwon
Is it impossible to debug like Linq to Sql as follow :http://msdn.microsoft.com/en-us/library/bb386961.aspx
kwon
or something like this : http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx
kwon
@kwon, if you want to see SQL in the Output window you can use show_sql option. I updated the answer.
Dmytrii Nagirniak
+1  A: 

You can use the NHibernate Profiler to do so, see this post for details on how to setup programmatic integration: http://ayende.com/Blog/archive/2009/12/13/uumlberprof-new-feature-programmatic-integration.aspx

Another option, slightly more complicated, is to listen to the NHibernate.SQL logger and process its results. It gives you the information, but you would need a bit of parsing and it won't give you the sessions information

Ayende Rahien