RhinoMock vs. TypeMock vs. NUnit 的模拟?

发布于 2024-08-03 19:40:27 字数 82 浏览 7 评论 0原文

我刚刚开始进行测试驱动开发,我想知道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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

把梦留给海 2024-08-10 19:40:27

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.

夜灵血窟げ 2024-08-10 19:40:27

名为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.

酒与心事 2024-08-10 19:40:27
  • Rhino.Mocks 是一个开源框架,由业界最多产的开发人员之一不断开发和改进。它已经存在了一段时间,因此支持多种不同的模拟范例。因此,从某种意义上说,学习可能会有点困难,因为您可能会找到“旧”做事方式的教程。这里有一个提示,SetUpResultFor()Expect.Call() 是旧的处理方式。新方法是mockObject.AssertWasCalled()

我没有与这些其他人有任何个人经验,但是...

  • MOQ 是一个开源的、由业内不太多产的开发人员之一(与 Ayende 相比)不断开发和改进的框架。它较新,因此缺少 Rhino.Mocks 所具有的一些功能。这通常不是问题,因为这些功能在 Rhino 中往往已被弃用。我听说,正因为如此,学习起来稍微容易一些(顺便说一句,模拟框架并不难学)。
  • 就模拟而言,NUnit Mocks 非常古怪。它不支持当前首选的 Arrange-Act-Assert 语法,而是依赖于 Expect-Verify(记录/重播)。它还依赖字符串来识别方法和属性名称,而不是 lambda。这使得它对重构有很大的抵抗力。这是一个严重的问题。我不会推荐它。
  • TypeMock Isolate 是一个硬核付费模拟框架,来自 Roy Osherove 公司(拥有者?),Roy Osherove 知道他的测试,但对于如何应用它也有一些略有争议的观点。就它能做的事情而言,它确实非常激烈 - 深入到底层并修改 CLR 对象的工作方式。然而,TypeMock 背后的理念并不是真正的 100% TDD。 TDD 的部分好处是,通过接受 Mocking 框架的限制,您将设计出更好的代码。 TypeMock 彻底打破了这些限制。据我所知,它主要由那些试图获取他们无法控制的测试代码的人使用。
  • Rhino.Mocks is an open source, continuously developed and improving framework by one of the industry's most prolific developers. It has been around for a while and hence supports quite a few different paradigms for mocking. It can be a little tougher to learn therefore in the sense that you might find tutorials for the "old" way of doing things. Here's a tip, SetUpResultFor() and Expect.Call() are the old ways of doing things. The new way is mockObject.AssertWasCalled().

I have not had any personal experience with these others but...

  • MOQ is an open source, continuously developed and improving framework by one of the industry's somewhat less prolific developers (as compared to Ayende). It is newer and therefore lacks some features that Rhino.Mocks has. This is usually not a problem since these features tend to be ones that are somewhat deprecated in Rhino. I have heard that because of this it is slightly easier to learn (mocking frameworks aren't hard to learn by the way).
  • NUnit Mocks is very quaint as far as mocking goes. It doesn't support the currently preferred Arrange-Act-Assert syntax relying instead on Expect-Verify (record/replay). It also relies on strings to identify method and property names instead of lambdas. This makes it significantly resistant to refactoring. This is a serious problem. I would not recommend it.
  • TypeMock Isolater is a hardcore for-pay mocking framework from a company (owned by?) Roy Osherove - a guy who knows his testing but also has some mildly controversial opinions as to how to apply it. It is really intense as far as what it can do - getting down to the low level and modifying how CLR objects work. The philosophy behind TypeMock isn't really 100% TDD however. Part of the benefits of TDD is that by embracing the limitations of Mocking frameworks you will design better code. TypeMock blasts those limitations to pieces. As far as I know it is mostly used by people who are trying to get code they have no control over under test.
小…红帽 2024-08-10 19:40:27

我一直使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文