c# - How to Moq NHibernate extension methods? -
i'm developing application using nhibernate orm, nunit unit testing , ninject di. i'm mocking isession so:
var session = new mock<isession>(); with regular non-mocked session objects can query them linq extension methods this:
var result = session.query<myentity>(); but when try mock following code...
session.setup(s => s.query<myentity>()); ...i runtime "not supported" exception:
expression references method not belong mocked object: s => s.query<myentity>() how can mock basic queries in moq/nhibernate?
query<t>() extension method, that's why can't mock it. although @roger answer way go, it's useful have specific tests. can start investigating query<t>() method - either reading nhibernate code, or using own tests, , set appropriate methods on isession.
warning: creating such setup make test fragile, , break, if internal implementation of nhibernate changes.
anyway, can start investigation with:
var mocksession = new mock<isession>(mockbehavior.strict); //this make mock throw on each invocation not setup var entities = mocksession.object.query<myentity>(); the second line above going throw exception , show actual property/method on isession query<t>() extension method tries access, can set accordingly. keep going way, , have setup session can use in test.
note: i'm not familiar nhibernate, have used above approach when had deal extension methods other libraries.
Comments
Post a Comment