0

I am trying to use OCMock's andDo with a block but every time I try it, I get EXC_BAD_ACCESS. Below is an example with NSNotificationCenter but I get the same result for other objects using blocks.

What am I missing?

[[[notificationCenterMock stub] andDo:^(NSInvocation *invocation) {

        void (^block)(NSNotification *note);
        [invocation getArgument:&block atIndex:2];

        NSNotification *notification = [NSNotification notificationWithName:UIApplicationDidEnterBackgroundNotification object:nil];

        block(notification); //EXC_BAD_ACCESS when calling this line

    }] addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:[OCMArg any] usingBlock:[OCMArg any]];
BlueVoodoo
  • 3,626
  • 5
  • 29
  • 37

2 Answers2

1

Without looking into this too deeply, and without seeing more code, I think you might have to use __unsafe_unretained on the block variable. See here for more: EXC_BAD_ACCESS when accessing parameters in andDo of OCMock

Community
  • 1
  • 1
Erik Doernenburg
  • 2,933
  • 18
  • 21
0

Try telling the invocation to retain its arguments:

[[[notificationCenterMock stub] andDo:^(NSInvocation *invocation) {
    [invocation retainArguments];

    void (^block)(NSNotification *note);
    [invocation getArgument:&block atIndex:2];
    // …