Search code examples
c#unit-testingnhibernatemoqqueryover

Mocking out nHibernate QueryOver with Moq


The following line fails with a null reference, when testing:

var awards = _session.QueryOver<Body>().Where(x => x.BusinessId == (int)business).List();

My test is like so:

var mockQueryOver = new Mock<IQueryOver<Body, Body>>();
mockQueryOver.Setup(q => q.List()).Returns(new List<Body> {_awardingBody});
_mockSession.Setup(c => c.QueryOver<Body>()).Returns((mockQueryOver.Object));
_mockCommandRunner = new Mock<ICommandRunner>();
_generator = new CertificateGeneratorForOpenSSLCommandLine(_mockSession.Object, _mockCommandRunner.Object, _mockDirectory.Object, _mockFile.Object, _mockConfig.Object); 

To be honest I'm flailing around in the dark here - I'm relatively new to nHibernate and Moq, so I'm not very sure what to google to get the right information.


Solution

  • I don't think the above code is right. AFAIK QueryOver is an extension method on ISession interface and you can not Mock extensions method like that (at least not with conventional Mocking tools like Moq or RhinoMocks).