EXC_BAD_ACCESS when accessing parameters in andDo of OCMock EXC_BAD_ACCESS when accessing parameters in andDo of OCMock xcode xcode

EXC_BAD_ACCESS when accessing parameters in andDo of OCMock


I ran into a similar problem when using NSProxy's forwardInvocation: method.

Can you try the below?

NSInvocationOperation *op; // Change this line__unsafe_unretained NSInvocationOperation *op; // to this line

Or another approach could be to retain NSInvocation's arguments:

[invocation retainArguments];

http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSInvocation_Class/Reference/Reference.html#//apple_ref/occ/instm/NSInvocation/retainArguments

I'll try to add a more detailed explanation later.


I think the problem is that you're trying to invoke a mock object directly. For what you're trying to do, you shouldn't need a mock object. Just call the method and verify that the image was set:

expect(myObject.imageView.image).to.beNil();[myObject do_something_that_calls_setImage];expect(myObject.imageView.image).not.to.beNil();

If you really want to use a mock for some reason, you could do it with a real UIImageView and a partial mock:

UIImageView *imageView = myObject.imageView;id mockView = [OCMockObject partialMockForObject:imageView];__block BOOL imageSet = NO;[[[mockView stub] andDo:^(NSInvocation *invocation) {      UIImage *img;      [invocation getArgument:&img atIndex:2];      imageSet = (img != nil);  }] setImage:OCMOCK_ANY];[myObject do_something_that_calls_setImage];expect(imageSet).to.beTruthy();


In my case this was happening because I introduced another parameter to this method, so the block parameter got shifted by one.

I fixed it by changing [inv getArgument:&op atIndex:2] to [inv getArgument:&op atIndex:3]