gpt4 book ai didi

.net - 垫片和 stub 之间的区别

转载 作者:行者123 更新时间:2023-12-02 06:14:57 24 4
gpt4 key购买 nike

谁能非常清楚地告诉我在单元测试期间 shimstub 之间的主要区别是什么?

我了解模拟对象,并且阅读了有关填充程序和 stub 的信息,但仍然不清楚在哪种情况下我应该使用填充程序或 stub 。

最佳答案

让我引用 Martin Fowler 的文章 Mocks Aren't Stubs :

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

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

因此,如果违反期望,模拟可以直接使测试失败。 stub 不会这样做。

Shims(或 Moles)与它们的不同之处在于它们可以用来替换硬编码的依赖项,例如静态方法。在我看来,您应该避免这种情况,而更喜欢重构,这使得这些依赖项可以替换。请参阅this thread供进一步讨论,特别是吉姆·库珀的回答。

关于.net - 垫片和 stub 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28325053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com