2

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.

CodeReaper
  • 5,988
  • 3
  • 35
  • 56
  • this is probably discussed also in this other thread http://stackoverflow.com/questions/10653531/ocmock-an-nsoperation – Vik Aug 08 '13 at 09:07
  • @Vik Sigh. I have seen that question so many times in my attempts of fixing the issue, but never actually spotted the solution. Thanks, would you place an answer saying I should just add the original operation variable to the queue and it works and still mocks? – CodeReaper Aug 08 '13 at 09:13

1 Answers1

0

As suggested in my previous comment, you can add the original operation variable to the queue

Vik
  • 1,897
  • 12
  • 18