allending/Kiwi — pull request #214 by stepanhruda (2013-02-09 17:14:43+00:00)
Title: Added method for block substitution capabilities to all NSObjects

Substituting method for blocks proves to be very useful not just for mocks.

Use case: Saving a managed object context

When using `NSManagedObject` subclasses in your specs, you typically end up with an actual `NSManagedObjectContext` instance filled by actual managed objects, because these cannot be easily mocked.

When testing a method that calls `save:` on the managed object context, your tests should be covering the save going successfully (returning `YES` and not filling in the passed error pointer) as well as covering a save failure (which usually means some Core Data validations went wrong, returning `NO` and filling in the pointer). You should not set these successful and failed contexts based on your actual models' validation, because these can easily change and then the tests will fail and need to be rewritten, even though the tested code works.

Simply stubbing the method return value does not help, if your method is not interested in the save's return value, but the actual error (which is the right thing to do). With these new methods, you can easily fill in whichever error you want in the block you provide.