0

I have a method from a class under tests which takes two delegates: the second delegate will call a method after the first delegate will be called with a callback function as input.

@implemenation ClassUnderTest
... 


- (void) methodWithMultipleCallbacks: (id<MyDelegate>) delegate
              withSecondDelegate: (id<MyDelegateWithCallback>) delegateWithCallback {    
    for (int i = 0; i < 2; i++){
        Callback callback = ^(int input) {
            NSLog(@"%d-th callback", i);
            NSLog(@"input = %d", input);
        };
        [delegateWithCallback fetchInt:callback];
    }
    [delegate delegateDoStuff:32];
}

But the strange thing happens: I tried to test it using OCMock, mocking both delegates, but the crashes, and I got a EXEC_BAD_ACCESS. I am utterly confused and would really appreciate any help here! Here's the test function

- (void) testWithMultipleCallbacks {
    id <MyDelegateWithCallback> mockDelegateWithCallback = OCMProtocolMock(@protocol(MyDelegateWithCallback));
    OCMStub([mockDelegateWithCallback fetchInt:[OCMArg any]]).andDo(^(NSInvocation *invocation) {
      void (^block)(int) = NULL;
      [invocation getArgument:&block atIndex:2];
      NSLog(@"got here"); 
      block(33); 
    });
    [_classUnderTest methodWithMultipleCallbacks: _mockedDelegate withSecondDelegate: mockDelegateWithCallback];
    OCMVerify(OCMTimes(1), [_mockedDelegate delegateDoStuff:[OCMArg any]]);
}
hao chen
  • 165
  • 1
  • 7

0 Answers0