Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCMock an NSOperation

I am trying to write some Unit Tests to test some custom NSOperations that we are writing. What I'd like to do is create a Mock of the NSOperation and put it on the NSOperationQueue, and then wait for it to finish. I know I can swizzle the methods and not use OCMock at all, but I really don't want to do that. I'd like to use OCMock. The code I'm trying to run is something like the following:

MYOperation *operation = [MYOperation new];
id mockOperation = [OCMockObject partialMockForObject:operation];
[NSOperationQueue *queue = [NSOperationQueue new];
[queue setMaxConcurrentOperationCount:1];
[queue addOperation:mockOperation];

When the unit test gets to this line:

[queue addOperation:mockOperation];

I get a call to a deallocated object exception. Anyone have any suggestions on how I can overcome this?

like image 797
Nick Cipollina Avatar asked Nov 03 '22 23:11

Nick Cipollina


1 Answers

If you're using ARC, operation is probably released right after you create the mock, as it's not accessed again. If you change it to this, it should fix the error:

[queue addOperation:operation];

...which you should be doing anyways--you're testing your object, not the mock.

like image 79
Christopher Pickslay Avatar answered Nov 15 '22 06:11

Christopher Pickslay