views:

191

answers:

1

How would you test the following code?

public IList<T> Find(DetachedCriteria criteria)
{      
    return criteria.GetExecutableCriteria(session).List<T>();
}

I would like to mock NH implementation (like setting mocks for ISession, ISessionFactory etc.) but I am having trouble with this one.

+1  A: 

You shouldn't really test this as that would be testing NHibernate. As a matter of fact, you can see very similar unit tests in NH source code itself.

If you wanted to test some other code that uses this code, here's how you'd stub it:

Db.Stub(x => x.Find(Arg<DetachedCriteria>.Is.Anything))).Return(new List<Blah>{new Blah()});

In my experience, if you want to test your queries (e.g. the ones that build the DetachedCriteria) you are much better of with an in-memory DB like SQLite, or better yet, a real SQL Server instance (or SQL Server CE for in-memory).

zvolkov
I was testing the repository implementation itself.Your're right, there is nothing to test here, it would be testing NHibernate, I should probably focus on testing queries.Thanks!