OCMockito mocking calls with block
objective-c, ocmockito, unit-testing
Solution
Scroll down to the bottom of https://github.com/jonreid/OCMockito and you'll see "Capturing arguments for further assertions". The second example shows how to use `MKTArgumentCaptor` to capture a block argument, then call it.
Here's an example:
MKTArgumentCaptor *argument = [[MKTArgumentCaptor alloc] init];
[verify(mockObject) createWithCompletion:[argument capture]];
void (^completion)(FuseResult *result, NSError *err) = [argument value];
completion(someResult, someErr);
This doesn't make `mockObject` call the block in any way. Instead, it captures the block passed to `mockObject`. The final step is to call the captured block with whatever arguments you want for your test.
Problem
I want to mock an object with the following message declaration: `- (void)createWithCompletion:(void (^)(FuseResult *result, NSError *err)) completion;` Is it possible to mock the block call this message has to handle? I read the ArgumentCaptorTest which has block but I wasn't sure if it's relevant.