NHibernate 可查询单元测试
然而,在尝试模拟返回 IQueryable 的数据访问方法时,尝试对 NHibernate 和工作单元模式进行单元测试却遇到了障碍?
这有效:
var employee = Helper.GetEmployee();
Repository.Stub(x => x.FindById<Employee>(employee.Id)).Return(employee);
这不起作用:
var employee = Helper.GetEmployee();
var employeeList = new List<Employee> { employee };
Repository.Stub(x => x.All<Employee>().ToList()).Return(employeeList);
基本上,任何返回 > 的内容都可以。 1 员工的行为举止我不能被嘲笑。
存储库 FindById 方法返回:
Session.Get<TEntity>(id);
存储库 All 方法返回:
Session.Query<TEntity>();
当模拟存储库 All 方法运行单元测试时,返回异常说源不能为空?
我被困住了,有什么想法吗?
谢谢! 蒂姆
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过以下操作。您的数据访问方法返回 IQueryable,但您试图模拟对结果调用 ToList() 时返回的内容?你应该只是嘲笑这个结果。
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.