【问题标题】:OCMockObserver: unexpected notification observedOCMockObserver:观察到意外通知
【发布时间】:2014-01-07 19:59:54
【问题描述】:

NotificationManager.h

#import <Foundation/Foundation.h>

@interface NotificationManager : NSObject

-(void)postNotification;

@end

NotificationManager.m

#import "NotificationManager.h"

@implementation NotificationManager

-(void)postNotification
{
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@"Some data" forKey:@"TestData"];
    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"TestNotification" object:nil userInfo:userInfo]];
}

@end

单元测试:

-(void)testNotification
{
    id observerMock = [OCMockObject observerMock];

    [[NSNotificationCenter defaultCenter]addMockObserver:observerMock name:@"TestNotification"  object:nil];

    [[observerMock expect] notificationWithName:@"TestNotification" object:[OCMArg any]];

    NotificationManager * nm= [[NotificationManager alloc]init];
    [nm postNotification];

    [observerMock verify];

    [[NSNotificationCenter defaultCenter] removeObserver:observerMock];
}

我得到错误:

OCMockObserver:观察到意外通知:NSConcreteNotification 0xfbbad70 {name = TestNotification;用户信息 = { TestData = "一些数据"; }}

如果我在没有 userInfo 对象的情况下发布通知(只是 nil),则测试有效。有人可以解释为什么吗?

【问题讨论】:

  • 这是一个结构很好的问题。我只花了几秒钟就用您发布的代码重新创建了您的问题。

标签: objective-c ocmock xctest


【解决方案1】:

当您不指定 userInfo 时,它期望值为零。改成:

[[observerMock expect] notificationWithName:@"TestNotification" object:[OCMArg any] userInfo:[OCMArg any]];

应该会过去的。

【讨论】:

    猜你喜欢
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多