gpt4 book ai didi

unit-testing - 模拟框架中的模拟与 spy 事件

转载 作者:行者123 更新时间:2023-12-03 04:37:00 26 4
gpt4 key购买 nike

在模拟框架中,您可以模拟一个对象或监视它。两者之间有什么区别?我什么时候会/应该使用其中一种而不是另一种?

查看Mockito ,例如,我看到使用 spiesmocks 完成类似的事情,但我不确定两者之间的区别。

最佳答案

模拟对象完全替换模拟类,返回记录值或默认值。您可以“凭空”创建模拟。这是单元测试期间最常用的。

当进行 spy 事件时,您可以使用现有对象并仅“替换”某些方法。当您有一个庞大的类并且只想模拟某些方法(部分模拟)时,这非常有用。让我引用一下Mockito documentation :

You can create spies of real objects. When you use the spy then the real methods are called (unless a method was stubbed).

Real spies should be used carefully and occasionally, for example when dealing with legacy code.

如有疑问,请使用模拟。

关于unit-testing - 模拟框架中的模拟与 spy 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12827580/

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