首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试NSNotification交付

测试NSNotification交付
EN

Stack Overflow用户
提问于 2013-03-27 00:28:59
回答 1查看 271关注 0票数 1

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

我得到了这个错误:

代码语言:javascript
复制
error: -[APHIssueComposerTests testPopulatedIssueIsReceived] : OCMockObject[APHIssueComposerTests]: expected method was not invoked: reportIssueNotificationReceived

在APHIssueComposer.m中:

代码语言:javascript
复制
- (void) reportIssue {
  APHIssue* issue = [self issue];

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

  [self discardIssue];
}

在APHIssueComposerTests.m中:

代码语言:javascript
复制
- (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]; 
}

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

EN

回答 1

Stack Overflow用户

发布于 2013-03-27 01:33:03

问题是enqueueNotification是异步的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15642344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档