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 に答える