Question

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.

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top