垫片和存根之间的区别 [英] Difference between shims and stubs

查看:107
本文介绍了垫片和存根之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能清楚地告诉我 shim stub 之间的主要区别是什么吗?

Can anyone tell me very clearly what's the main difference between a shim and a stub during unit testing?

我了解模拟对象,并且阅读了有关垫片和存根的信息,但仍不清楚在哪种情况下应该使用垫片或存根。

I know about mock objects and I read about shims and stubs but it's still not clear in which context I should use a shim or a stub.

推荐答案

让我引用Martin Fowler的文章不是存根的小插曲

Let me cite Martin Fowler's article Mocks Aren't Stubs:


存根可为在通话过程中拨打的电话提供罐装答案测试,通常
不会对
测试中所编程的内容做出任何反应。存根可能还会记录有关呼叫的信息,例如,电子邮件
网关存根会记住其发送的消息,或者可能仅记录其发送的消息数量。

Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'.

模拟是已预先编程的
对象,它们具有期望值,形成了
的调用规范

Mocks are [...] objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

因此,如果违反了预期,模拟程序可以直接使测试失败。存根不这样做。

So mocks can directly make a test fail if an expectation is violated. Stubs don't do that.

垫片(或Moles)与它们两者的不同之处在于它们可用于替换硬编码依赖,如静态方法。您应该避免使用IMO,而应该使用重构,这样可以使这些依赖项可替换。请参阅此主题,以进行进一步的讨论,尤其是吉姆·库珀的答案。

Shims (or Moles) differ from both of them in that they can be used to replace hard-coded dependencies like static methods. You should avoid that IMO and prefer a refactoring, which makes these dependencies replaceable. See this thread for further discussion, especially Jim Cooper's answer.

这篇关于垫片和存根之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆