1

I'm trying to make sure the NSNotification gets sent after reportIssue is called.

I get this error:

error: -[APHIssueComposerTests testPopulatedIssueIsReceived] : OCMockObject[APHIssueComposerTests]: expected method was not invoked: reportIssueNotificationReceived

In APHIssueComposer.m:

- (void) reportIssue {
  APHIssue* issue = [self issue];

  NSNotification* notification = [NSNotification notificationWithName:APHLogDataObjectNotification object:issue];
  [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostWhenIdle];

  [self discardIssue];
}

In APHIssueComposerTests.m:

- (void)setUp
{
  [super setUp];
  self.mockObserver = [OCMockObject mockForClass:[self class]];
  [[NSNotificationCenter defaultCenter] addObserver:self.mockObserver
                                           selector:@selector(reportIssueNotificationReceived)
                                               name:APHLogDataObjectNotification
                                             object:nil];
  self.issueComposer = [[APHIssueComposer alloc] initWithTempDirectory:@"/my/fake/directory"];
}

- (void)testPopulatedIssueIsReceived
{
  [[self.mockObserver expect] reportIssueNotificationReceived];
  self.issueComposer.message = @"fake message.";
  [self.issueComposer reportIssue];
  [mockObserver verify];
  [[NSNotificationCenter defaultCenter] removeObserver:mockObserver name:APHLogDataObjectNotification object:nil];
}

- (void)tearDown
{
  [super tearDown];
  [[NSNotificationCenter defaultCenter] removeObserver:mockObserver name:APHLogDataObjectNotification object:nil]; 
}

Why doesn't the mock object receive the notification?

Rose Perrone
  • 61,572
  • 58
  • 208
  • 243

1 Answers1

0

The problem is that enqueueNotification is asynchronous.

Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
  • Yes. For these situations I think it makes sense to andDo: block to your mockObserver that sets a __block BOOL, and then spin-wait with a timeout on that value. – Ben Flynn Apr 24 '13 at 21:35