0

I'm using Kiwi on a project and am not quite clear on when to use a mock versus a nullMock. Here is what Kiwi's documentation currently provides:

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).

It what kind of scenario would this difference come into play?

H K
  • 1,215
  • 2
  • 16
  • 29

1 Answers1

0

Null mocks come with the nice feature of responding to every possible message being sent to them, this means that you don't need to manually setup every method that will get called on that mock. Regular mocks don't have this, you will need to prepare them if you'll be injecting them as dependencies.

Most of the cases you'll use a null mock as you're usually interested if a certain method got called, as anyway you're testing the other methods on the mock in other tests.

This comes with advantages ad disadvantages:

  • tests using null mocks might hide subtle bugs as you might forget to test a corner case triggered by one of the "other" methods
  • tests using null mocks are developed faster as you don't have to track every method that the tested method calls, which is also not good as you create a strong relationship between the test and your code, that makes the refactoring harder
Cristik
  • 30,989
  • 25
  • 91
  • 127