allending/Kiwi — pull request #187 by eraserhd (2013-01-08 19:52:49+00:00)
Title: Mock for dependency

This introduces a category on NSObject with the following methods:

- mockFor:
- nullMockFor:

To handle corner cases, the following methods are also provided:

- mockFor:ofType:
- mockFor:conformingToProtocol:
- nullMockFor:ofType:
- nullMockFor:conformingToProtocol:

These methods stub out a dependency (currently an instance variable) with an equivalent mock. For example:

``` objc
[[[[cruiser mockFor:@"engine"] should] receive:@selector(model) andReturn:@"foo"];
```

It will make protocol mocks (for `id<*>`-type instance variables), class mocks (for `FooClass*`-type instance variables), an NSObject mock (for a simple `id` instance variable). If the dependency does not exist or it doesn't know how to create an equivalent mock, it will throw exceptions with useful messages.

Calling -mockFor: or -nullMockFor: twice for the same dependency on the same object will return the same mock.

*NOTE:* This was made considerably easier by extracting the instance variables of KWMock which specify its behavior into a KWMockDescription class.