测试NSNotification交付

时间:2013-03-26 16:28:59

标签: nsnotificationcenter ocmock sentestingkit

我正在尝试确保在NSNotification被调用后发送reportIssue

我收到此错误:

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

在APHIssueComposer.m中:

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

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

  [self discardIssue];
}

在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]; 
}

为什么模拟对象不接收通知?

1 个答案:

答案 0 :(得分:1)

问题是enqueueNotification是异步的。