1

我正在尝试确保在调用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]; 
}

为什么模拟对象没有收到通知?

4

1 回答 1

1

问题是这enqueueNotification是异步的。

于 2013-03-26T17:33:02.750 回答