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:

  1. 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
  2. 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

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -