Kiwi:mock 和 nullMock 之间的具体区别?

Kiwi: concrete difference between mock and nullMock?

我在项目中使用 Kiwi,不太清楚何时使用 mocknullMock。以下是 Kiwi 的文档当前提供的内容:

A plain mock object will raise an exception when it receives a selector or message pattern that it does not expect. Expected messages are automatically added to a mock when stub or receive expectations are used on a mock.

If you don't care about mocks receiving other messages, and don't want exceptions to be raised, then use a null mock (also known as a null object).

这种差异会在什么样的情况下发挥作用?

空模拟具有响应发送给它们的每条可能消息的良好功能,这意味着您不需要手动设置将在该模拟上调用的每个方法。常规模拟没有这个,如果您将它们作为依赖项注入,则需要准备它们。

大多数情况下,您将使用 null mock,因为您通常对某个方法是否被调用感兴趣,因为无论如何您都在其他测试的模拟中测试其他方法。

这有优点也有缺点:

  • 使用 null mocks 的测试可能会隐藏细微的错误,因为您可能会忘记测试由 "other" 方法之一触发的极端情况
  • 使用 null mocks 的测试开发速度更快,因为您不必跟踪被测试方法调用的每个方法,这也不好,因为您在测试和代码之间建立了牢固的关系,即使重构更加困难