문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top