Reputation: 6969
Trying to unit test NHibernate and unit of work pattern however have come up against a brick wall when trying to mock data access methods which return an IQueryable?
This works:
var employee = Helper.GetEmployee();
Repository.Stub(x => x.FindById<Employee>(employee.Id)).Return(employee);
This doesn't work:
var employee = Helper.GetEmployee();
var employeeList = new List<Employee> { employee };
Repository.Stub(x => x.All<Employee>().ToList()).Return(employeeList);
Basically, anything which returns > 1 employee I can't get mock to behave.
Repository FindById method returns:
Session.Get<TEntity>(id);
Repository All method returns:
Session.Query<TEntity>();
When unit test runs of mocked repository All method, returns exception saying source can't be null?
I'm stuck, any ideas?
Thanks! Tim
Upvotes: 2
Views: 388
Reputation: 2096
Have you tried the following. Your data access method is returning an IQueryable, but you're trying to mock what gets returned when you call ToList() on the result? You should just be mocking the result.
Repository.Stub(x => x.All<Employee>()).Return(employeeList.AsQueryable<Employee>);
Upvotes: 1