RhinoMock vs. TypeMock vs. NUnit 的模拟?
我刚刚开始进行测试驱动开发,我想知道RhinoMock、TypeMock 和NUnit 内置模拟之间的主要区别?
任何信息将不胜感激!
I am just starting to do Test Driven Development, and I am wondering the major differences between RhinoMock, TypeMock, and NUnit's built-in mocking?
Any information would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
TypeMock 是一种商业产品(意味着您必须为其付费),但允许您模拟具体对象 - 与只能模拟接口/抽象类的 RhinoMocks/NUnit/MoQ 不同。它如何实现这一点几乎是黑魔法,但它使用 CLR 做了一些非常聪明的事情。
当您在项目中使用不使用很多接口的库时,这尤其有用。例如,您可以使用 TypeMock 模拟 LINQtoSQL 数据上下文或 Sharepoint 对象。但是,如果您使用 TypeMock,那么这不是您应用程序中糟糕设计的借口。
据我所知,除了细微的语法差异之外,大多数模拟框架已经脱离了旧的记录/回放模型。通常,您可以通过使用 Fluent Interface 编写期望来设置模拟。
就我个人而言,我只使用过 MoQ 并且我 <3。
TypeMock is a commercial product (meaning you'll have to pay for it) but will allow you to mock concrete objects - unlike RhinoMocks/NUnit/MoQ which can only mock an interface/abstract class. How it achieves this is borderline black magic, but it does some very clever things with the CLR.
This can be particularly useful when you use libraries in your project that don't use many interfaces. So you could, for example, use TypeMock to mock out a LINQtoSQL datacontext, or Sharepoint objects. However, if you are using TypeMock this is no excuse for bad design in your application.
As far as I'm aware, aside from minor syntax differences, most of the mocking frameworks have moved away from the old record/playback model. Commonly, you set up your mocks by writing expectations using a Fluent Interface.
Personally, I have only used MoQ and I <3 it.
名为TDD - 了解模拟对象 Roy Osherove 的文章对于学习不同模拟库的差异非常有帮助。他并没有把每一个方面都讲得很详细,但足以让你理解。我希望这有帮助。 Roy 也是 TypeMock 的首席架构师,是单元测试领域非常有影响力的人物。对于那些想要学习如何使用模拟并了解该库的可用内容的人,我极力推荐此视频。
TypeMock 和开源库之间的主要区别在于 TypeMock 使用 Microsoft 提供的 Profiler API,而不是 动态代理。这允许 TypeMock 模拟具体类和静态方法。如果您不确定探查器是什么,它与 JetBrain 的 dotTrace 和 RedGate 的 Ants .Net 探查器等工具使用的 API 相同。 TypeMock 只是以不同的方式使用 API 来伪造(模拟)您告诉它的内容。
@RichardOD,感谢您的提醒,他的书“单元测试的艺术”更详细地介绍了其中视频没有。我拥有这本书,内容非常丰富。
A video called TDD - Understanding Mock Objects by Roy Osherove is very helpful in learning the differences of the different mocking libraries. He doesn't go in great detail of every aspect, but enough for you to understand. I hope this helps. Roy is also the Chief Architect for TypeMock and is a very influential figure in the unit testing arena. I couldn't recommend this video enough for someone who wants to learn how to use mocking and also learn about the library's available.
The main difference between TypeMock and the open-source library's is that TypeMock uses the Profiler API provided by Microsoft instead of a dynamic proxy. This allows TypeMock to mock concrete classes and static methods. In case you aren't sure what the profiler is, it is the same API that is used by tools like JetBrain's dotTrace and RedGate's Ants .Net profilers. TypeMock just uses the API in a different way to fake(mock) what you tell it to.
@RichardOD, thanks for the reminder, his book "The Art of Unit Testing" goes into greater detail where the video doesn't. I own the book and it is very informative.
我没有与这些其他人有任何个人经验,但是...
I have not had any personal experience with these others but...
我一直使用 TypeMock,并发现它是一个非常强大的工具,可以提高单元测试的覆盖率。这是因为我使用 SharePoint,只有 TypeMock 可以允许我模拟 SharePoint 类 - 因为它们是具体的类而不是接口。
使用 RhinoMock、Moq、NUNit 等不可能模拟 SharePoint 类,因为(我相信)它们需要模拟对象的接口,而不是能够模拟实际的具体类。
如果您的代码确实使用了大量接口,并且不需要模拟具体类,那么 TypeMock 有点贵,但就您获得的功能而言,这是值得的。
I use TypeMock all the time and have found it to be a very powerful tool that can improve the coverage of my unit tests. This is because I work with SharePoint and only TypeMock can allow me to mock out SharePoint classes - since they are concrete classes and not interfaces.
Mocking SharePoint classes is not possible with RhinoMock, Moq, NUNit, etc since (I believe) they require interfaces to mock objects, rather then being able to mock the actual concrete classes.
If your code does use a lot of interfaces, and you don't require mocking concrete classes then TypeMock is a bit pricey, but for the power you get, it's worth it.