Query<T>() is an extension method, so you cannot mock it. Although @Roger's answer is a way to go, it is sometimes useful to have specific tests. You can start exploring what the Query<T>() method does - either by reading NHibernate code or using your own tests, and set the appropriate methods for ISession.
Warning: creating such a setting will make your test very fragile and it will break if the internal implementation of NHibernate changes.
In any case, you can start your investigation with:
var mockSession = new Mock<ISession>(MockBehavior.Strict); //this will make the mock to throw on each invocation which is not setup var entities = mockSession.Object.Query<MyEntity>();
The second line above will throw an exception and show you which actual property / method on ISession trying to use the Query<T>() extension method, so you can set it accordingly. Keep going this way, and you will end up with a good setup for your session so that you can use it in the test.
Note. I am not familiar with NHibernate, but I used the above approach when I had to deal with extension methods from other libraries.
source share