iosunit-testingtestingtddkiwi

Kiwi: concrete difference between mock and nullMock?


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?


Solution

  • 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: