I have the following code where I try to 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, this throws the exception:
System.InvalidOperationException : Type 'System.Linq.EnumerableQuery`1[[MIB.DomainModels.UnitModel, MIB, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' doesn't match the return type 'NHibernate.Engine.ISessionImplementor' for method 'ISession.GetSessionImplementation();'
What am I doing wrong here? How should I go about 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
is usually costly and painful.
Query<T>
, in particular, is an extension method that calls ISession.GetSessionImplementation()
internally, so you'd have to mock that to work with it (which is non-trivial and actually ties you to a specific implementation detail)
There are two better alternatives:
Unless you are already using repositories, my advice is to go with #2.