【问题标题】:OCMock notification observer - verifying the objectOCMock 通知观察者 - 验证对象
【发布时间】:2018-06-14 08:12:42
【问题描述】:

我最近开始使用 OCMock 框架并试图弄清楚如何使用通知观察器。所以在我的源代码中,我观察到一些通知:

typedef enum {
  OperationStatusCompleted,
  // Some other statuses
} SomeOperationStatus;

- (void)addObserverForSomeNotification {
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(someOperationStatusDidChange:)
                                               name:@"someOperationStatusDidChange"
                                             object:nil];

- (void)backgroundOperationStatusDidChange:(NSNotification *)notification {
  id<SomeOperationProtocol> operation = [notification object];
  if (operation.status == OperationStatusCompleted) {
    // Do something.
  }
}

现在我想在我的测试中添加对该通知的期望:

- (void)testNotification {
  id observerMock = OCMObserverMock();
  [[NSNotificationCenter defaultCenter] addMockObserver:observerMock name:@"someOperationStatusChanged" object:nil];
  [[observerMock expect] notificationWithName:@"someOperationStatusChanged" object:[OCMArg any]];
   // run the test ...
}

它本身工作正常,我在测试中收到通知,但我真正想要检查的是通知对象的 状态(这意味着我收到了通知具有特定状态的特定对象)。有可能吗?

【问题讨论】:

    标签: objective-c unit-testing ocmock


    【解决方案1】:

    我终于想通了:

    [[observerMock expect] notificationWithName:@"someOperationStatusChanged" object:[OCMArg checkWithBlock:^BOOL(id<IMBBackgroundOperation> param) { 
        return param.status == OperationStatusCompleted;
    }]];
    

    【讨论】:

      猜你喜欢
      • 2019-09-18
      • 2016-10-11
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多