如何模拟或类似地注入应用于 NHibernate 会话的查询扩展方法的替代方法
我正在尝试模拟或类似应用于 nhibernate-3 会话的查询扩展方法。类似于以下内容...
public IQueryable<Entity> GetEntities
{
return entities = Session.Query<Entity>();
}
其中 Query 扩展方法返回 IQueryable 集合。
我可以使用 Moq 或 Rhinos 清楚地模拟 Session 对象。但到目前为止还没有找到合适的方法来伪造或替换扩展方法。事实上,阅读有关堆栈溢出的几个问题的答案,这可能是不可能的。
查看问题 如何使用 Rhino Mock 模拟扩展方法? 使用 Moq 模拟扩展方法 以及其中的链接..
我遇到了 microsoft moles 项目,并认为也许我可以用它来创建一个替代程序集,通过它我可以测试这一特定方法。使用 Pex 生成这样的程序集相对相当容易,但是当我尝试使用该程序集的方法时,并没有立即清楚如何将其分解出来。
理想情况下,我只想检查以确保此方法调用查询扩展方法并返回抢占的结果。
有没有人做过类似的事情,想听听人们的想法
。有类型模拟,但我没有资金购买它:)
I am trying to mock out or similar the Query extension method applied to an nhibernate-3 session. Similar to the following...
public IQueryable<Entity> GetEntities
{
return entities = Session.Query<Entity>();
}
Where the Query extension method returns an IQueryable collection.
I can clearly mock out the Session object by using Moq or Rhinos. But as of yet not found a suitable way to fake out or replace the extension method. In fact reading answers to several questions on stack overflow, it may not be possible.
see questions
How to mock extension methods with Rhino Mock?
Mocking Extension Methods with Moq
and links within..
I came across the microsoft moles project and thought maybe I could use that to create a subsitute assembly by which I could test this one particular method. Using Pex its relatively quite easy to generate such an assembly however when I try to work a way of using this Assembly its not immediately clear of a way to factor this out.
Ideally I would like to just check to make sure that the query extension method is called by this method and a pre-empted result is returned..
Has anyone done anything similar, would like to hear peoples thoughts..
Ps. there is type mock but I don't have the funds to buy it :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
扩展方法是静态方法。所以你不能使用像 Rhino Mocks 或 Moq 这样的模拟库。 (这是 CLR 的限制。)TypeMock 或 Moles 通过将自己嵌入到探查器级别来绕过该限制。 (它们将自己注册为探查器,并将代码注入到您的应用程序域中。)
您可以使用与 NHibernate.Linq.LinqExtensionMethods 中相同的签名定义自己的扩展方法:
问题是它必须位于您的生产代码中周围有一些条件编译指令。
就我个人而言,我针对本地数据库或内存数据库(如 SQLite)对我的存储库进行集成测试。然后,我在测试更高级别的组件时模拟我的存储库。我不建议模拟 ISession、ISessionFactory 等。
Extension methods are static methods. So you can't use a mocking library like Rhino Mocks or Moq. (This is a CLR limitation.) TypeMock or Moles get around the limitation by wedging themselves in at the profiler level. (They register themselves as a profiler and inject code into your app domain.)
You can just define your own extension method with the same signature as found in NHibernate.Linq.LinqExtensionMethods:
The problem with this is it has to be in your production code with some conditional compilation directives around it.
Personally I integration test my repositories against a local database or an in-memory database like SQLite. I then mock out my repositories when testing higher level components. I wouldn't recommend mocking out ISession, ISessionFactory, and the like.