c# - Exception thrown when stubbing mocked `ISession` object -
i have following code try stub isession.query<unitmodel>().
var unitlist = new list<unitmodel>() { new unitmodel(){name = "meters", symbol="m"}, new unitmodel(){name="grams", symbol="g"} }; mockrepository.generatemock<isession>().stub(x => x.query<unitmodel>()).return(unitlist.asqueryable<unitmodel>()); when running, throws exception:
system.invalidoperationexception : type 'system.linq.enumerablequery`1[[mib.domainmodels.unitmodel, mib, version=1.0.0.0, culture=neutral, publickeytoken=null]]' doesn't match return type 'nhibernate.engine.isessionimplementor' method 'isession.getsessionimplementation();' what doing wrong here? how should go stubbing isession.query<unitmodel>()?
nhibernate 3.3.0.4000
edit: unitmodel class:
public class unitmodel { public virtual string name { get; set; } public virtual string symbol { get; set; } }
mocking isession costly , painful.
query<t>, in particular, extension method calls isession.getsessionimplementation() internally, you'd have mock work (which non-trivial , ties specific implementation detail)
there 2 better alternatives:
- implement repository interface has implementation backed nhibernate session, , can mocked
- pros: powerful, no limitations
- cons: might find limited own abstraction or reinventing wheel when need access specific nh features
- use in-memory or clean db testing
- pros: less work setup, have access nh features
- cons: if use different db engine, queries might have different behavior.
unless using repositories, advice go #2.
Comments
Post a Comment