Fulfill Expectation in an asynchronous Test

Extend the stubbing mechanism to let a stub fulfill an expectation of the asynchronous testing extension intruded in Xcode 6.

This extension lets you use a stub to fulfill an asynchronous test. This is useful, if you are testing a component and you have to wait until a certain delegate method has been called.

To use this, you have to enable the expectation matching (requires Xcode 6):

    #define MOCKITO_EXPECTATION
    #define MOCKITO_SHORTHAND
    #import <OCMockito/OCMockito.h>

In your test you can now mock for example a delegate protocol and wait until a delegate method has been called.

    // Prepare the stub and add an expectation …
    id <MyDelegate> delegate = mockProtocol(@protocol(MyDelegate));
    [given([delegate didFinish]) willFulfill:expectation(@"Did finish")];
   
    // … start you test ...
    
    // … and wait for the expectation.
    [self waitForExpectationsWithTimeout:20 handler:nil];

This extension works as expected in the project where I want to use it. But I'm not so familiar the insides of OCMockito, thus I don't know, if there is a better way to implement this feature.