2013-03-26 64 views
1

我試圖確保在調用reportIssue之後NSNotification被髮送。測試NS通知傳遞

我得到這個錯誤:

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

問題是enqueueNotification是異步的。

+0

是的。對於這些情況,我認爲這對於和Do有意義:阻止設置__block BOOL的mockObserver,然後在該值上超時旋轉等待。 – 2013-04-24 21:35:14