Error setting expectation on NSNotifciationCenter

Discussion of the OCMock framework. If you have patches we would prefer you to send them to the mailing list, but attaching them to a topic is possible, too.

Error setting expectation on NSNotifciationCenter

Postby tron_thomas » 15 Dec 2012, 20:08

I am trying to write a test like this:

Code: Select all
   id notifier = [OCMockObject mockForClass:[NotifyingObject class]];
   
   id centerMock = [OCMockObject partialMockForObject:[NSNotificationCenter defaultCenter]];
   [[centerMock expect] removeObserver:testedObject name:@"SomeNotification" object:nil];

   [[NSNotificationCenter defaultCenter] postNotificationName:@"SomeNotification" object:notifier];

   [centerMock verify];


When the postNotificationName:object: method is executed, the following error occurs:
'NSInternalInconsistencyException', reason: 'Ended up in subclass forwarder for NSNotificationCenter-0x8928740-377243556.858884 with unstubbed method removeObserver:name:object:'

When dealing with errors like this in the past, the problem was usually that the class being mocked did not implement the expected method. That doesn't make sense in this case because NSNotificationCenter does implement the removeObserver:name:object: method.

What is needed to eliminate this error?
tron_thomas
 
Posts: 19
Joined: 04 Mar 2012, 02:14

Re: Error setting expectation on NSNotifciationCenter

Postby tron_thomas » 25 Dec 2012, 21:14

I'm not sure why this problem was happening before. I have since been able to get things to work. One important thing to remember when mocking a singleton like NSNotificationCenter this way is to stop the mock for the singleton between tests.

I am encountering a new problem with this technique that involves an object that may try to register for multiple notifications. If a test sets an expectation only against a certain call to addObserver the test will fail with an error like:

Ended up in subclass forwarder for NSNotificationCenter-0x741ee70-378150153.101601 with unstubbed method addObserver:selector:name:object:

when other calls to addObserver occurs. This can be particularly problematic when testing a class that derives from another class and both classes make notification registrations.

What could someone do to allow the partial mock to ignore other calls to addObserver?
tron_thomas
 
Posts: 19
Joined: 04 Mar 2012, 02:14


Return to OCMock



cron