Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCMockito mocking calls with block

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.

like image 946
huggie Avatar asked Jan 23 '26 11:01

huggie


1 Answers

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.

like image 67
Jon Reid Avatar answered Jan 25 '26 09:01

Jon Reid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!