问题描述
有人能清楚地告诉我 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:
模拟是已预先编程的
对象,它们具有期望值,形成了
的调用规范
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.
这篇关于垫片和存根之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!