I was writing a unit test and it was giving me trouble. Turns out I can reproduce the trouble using just OCMock and normal obj-c. So I am sharing my simple test that reproduces my problem here in the hope that someone can help me understand why it is failing and possibly how I might write this test without crashing.
The error message is EXC_BAD_ACCESS(code=2,address=0x58)
-(void)testIfNSOperationIsMockable {
NSOperationQueue *queue = [NSOperationQueue new];
queue.maxConcurrentOperationCount = 1;
[queue setSuspended:YES];
NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"I am a simple operation.");
}];
id mockOperation = [OCMockObject partialMockForObject:operation];
[[mockOperation expect] cancel];
[queue addOperation:mockOperation]; // chrash happens on this line
[queue cancelAllOperations];
[queue setSuspended:NO];
[mockOperation verify];
}
I know that this might seem like a silly test, but in my real unit test the queue is wrapped in a manager, the operation is a custom implementation and the cancel is sent by the wrapper and targeting just one specific operation, so bear with me and my overly simple test.