进入存根和嘲笑

发布于 2024-10-02 19:31:35 字数 406 浏览 0 评论 0原文

我一直在考虑学习存根和模拟,并且我知道 .Net 的几个可用库:RhinoMocks、Moq、TypeMock...尽管我仍然不太清楚存根和模拟之间的区别,我知道它就在那里,我总是可以用谷歌搜索它。

我想知道,但不涉及 TDD 或类似的东西:

  • 学习曲线值得吗?我不太确定是否了解了有关期望的所有小细节,或者在这种情况下会得到什么回报。

  • 生产增益比手动制作“虚拟实现”大吗?我想是的,因为动态模拟/存根类更快。

  • 您会推荐哪个库?起订量似乎更容易。

  • 哪些是适合傻瓜的最佳资源(教程、书籍、视频...)并且或多或少是最新的?我不想了解库,当我下载最后一个版本时,意识到显示的大多数 API 不再有效。

感谢一切。

I've been thinking about learning about stubs and mocks, and I know of several of the available libraries for .Net: RhinoMocks, Moq, TypeMock... and although I still don't have too clear the difference between stubs, and mocks, I know it is there, and I could always google about it.

I'd like to know, without being into TDD or anything similar:

  • Is the learning curve worth it? I'm not so sure learning all the minor details about expectations or whatever would pay back in this situation.

  • Is the production gain so bigger than manually making my "dummy implementations"? I guess so, since mocking/stubbing a class dynamically is faster.

  • Which library would you recommend? Moq seems the easier one.

  • Which are the best resources (tutorials, books, videos...) for dummies and more or less up to date? I wouldn't like to learn about a library, and when I download the last release, realize that most of the API shown is no longer valid.

Thanks for everything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

思慕 2024-10-09 19:31:35
  • 学习曲线值得吗? - 是的,这并不难。一旦您知道如何使用模拟框架,您将在单元测试中节省大量时间。

  • 比手动模拟更好? - 不一定,在某些情况下手动模拟可能会更好。但在我看来,模拟框架通常是用最少的代码最快完成任务的方法。尝试这两种方法,您就会知道在给定情况下哪种方法最好。

  • 哪个图书馆? - MoqRhino Mocks 是最流行的 .Net 根据此民意调查,两者都具有丰富的功能且易于使用。使用 Rhino 有两种模拟方式(“记录和回放”和“AAA”模式)——我只使用 AAA 模式。我认为 Moq 仅适用于 Rhino 'AAA' 模式,因此可能不会那么混乱。

  • 不确定具体的资源,只需选择一个工具并 Google 一下,您就会找到很多。

就模拟和模拟之间的区别而言存根,模拟可以记录它发生的事情(例如调用了哪些方法),而存根则不能。当您开始使用它们时,您会发现更多。

  • Learning curve worth it? - Yes, it's not that hard. Once you know how to use a mocking framework you'll save a load of time in unit testing.

  • Better than manual mocks? - Not necessarily, in some situations manual mocking can be better. But IMO, mocking frameworks are usually the fastest way to get things done, with the least code. Try both approaches and you'll get to know which is best in a given situation.

  • Which library? - Moq and Rhino Mocks are the most popular for .Net according to this poll, and both have plenty features and are easy to use. With Rhino there are two ways of mocking ('record & playback' and 'AAA' mode) - I'd only use the AAA mode. I think Moq only works like Rhino 'AAA' mode, so that might be slightly less confusing.

  • Not sure about specific resources, just pick a tool and google it, you'll find plenty.

In terms of the difference between mocks & stubs, a mock can record what happened to it (e.g. what methods were called on it), whereas a stub can't. You'll find out more when you start using them.

花开雨落又逢春i 2024-10-09 19:31:35

我已经使用 FakeItEasy 几个月了,它真是太神奇了!
我极力推荐它。

I've been using FakeItEasy for a couple of months now, and it is Faking Amazing!
I can't recommend it highly enough.

不必你懂 2024-10-09 19:31:35

我实际上使用 RhinoMocks,虽然学习曲线相当痛苦,但现在我'我对结果非常满意。不管怎样,这需要(或者,至少,它花了我,但也许我有点笨:))一段时间,然后你才能比手动制作虚拟实现更快。我从未使用过你提到的任何其他库,所以我无法对它们进行比较。

主页还包含一些很好的文档。

总的来说,我认为这是一次值得的努力,我会推荐它。

I actually use RhinoMocks, and while the learning curve is quite painful, now I'm very happy with the results. Anyway, it takes (or, at least, it took me but maybe I'm a little dumb :)) some time before you get faster than manually making dummy implementations. I never used any of the other libraries you mention, so I can't make a comparison between them.

The homepage contains also some good documentation.

Overall, I think it was a worthwhile effort and I'd recommend it.

冷…雨湿花 2024-10-09 19:31:35

存根 - 模仿“真实”对象行为并负责保持测试可运行的假对象。

模拟 - 用于断言测试是否通过的存根。


关于模拟框架 - 这个问题之前已被问过。

我自己的偏好是使用这两种方法。当我觉得使用框架更容易时,我就会选择它,反之亦然 - 有时简单的假货就足够了,而且更容易理解。不幸的是 - 在您真正学习使用之前,您将无法自己做出判断。

Moq 最近似乎是 .Net 领域最流行的模拟框架。我也在用它。

Stub - fake object that mimics behavior of "real" object and is responsible of keeping test runnable.

Mock - stub that is used to assert if test passes.


About mocking frameworks - this question has been asked before.

My own preference is to use both approaches. When I feel using framework easier I go with that and vica versa - sometimes simple fakes are enough and easier to understand. Unfortunately - You won't be able to judge by yourself until You actually learn using one.

Moq lately seems to be most trending mocking framework at .Net space. I'm using it too.

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