2013-03-26 70 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