发布通知的 OCUnit 测试用例

Posted

技术标签:

【中文标题】发布通知的 OCUnit 测试用例【英文标题】:Issue OCUnit Test Case for Notification 【发布时间】:2013-03-04 13:25:48 【问题描述】:
- (id)init

    if (self = [super init])
    

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(onDidFinishLaunchingNotification:)
                                                     name:UIApplicationDidFinishLaunchingNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(onWillEnterForegroundNotification:)
                                                     name:UIApplicationWillEnterForegroundNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(onDidBecomeActiveNotification:)
                                                     name:UIApplicationDidBecomeActiveNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(onWillTerminateNotification:)
                                                     name:UIApplicationWillTerminateNotification
                                                   object:nil];
    

    return self;


// Notification Observers
- (void)onDidFinishLaunchingNotification:(NSNotification*)notification

    NSLog(@"onDidFinishLaunchingNotification");



- (void)onWillEnterForegroundNotification:(NSNotification*)notification

    NSLog(@"onWillEnterForegroundNotification");


- (void)onDidBecomeActiveNotification:(NSNotification*)notification

    NSLog(@"::onDidBecomeActiveNotification");


- (void)onWillTerminateNotification:(NSNotification*)notification

    NSLog(@"onWillTerminateNotification");

通知测试用例

    -(void)setup
        [super setUp];

    mClassObj = [[ClassA alloc]init];

    

-(void)teaddown

mClassObj = nil;
    [super tearDown];



 -(void)testUIApplicationDidFinishLaunchingNotification 

        [[NSNotificationCenter defaultCenter]postNotificationName:UIApplicationDidFinishLaunchingNotification object:nil];    
        

期待这会奏效!

但是测试用例失败了

-[__NSCFString onDidFinishLaunchingNotification:]: unrecognized selector sent to instance 

我正在尝试涵盖上述通知方法的测试用例 但它给了我错误,说无法识别的选择器发送到实例!

有人建议我覆盖通知方法的测试用例

@提前致谢

【问题讨论】:

【参考方案1】:

除了我认为setUptearDown 的意外拼写错误(注意大小写)之外,这段代码没有任何问题。我在onDidFinishLaunchingNotification: 上放了一个断点,它会被测试触发。

问题是,一个常量 NSString 是如何潜入其中的?

【讨论】:

以上是关于发布通知的 OCUnit 测试用例的主要内容,如果未能解决你的问题,请参考以下文章

OCUnit - 测试用例失败时中断?

如何编写 OCUnit 测试用例

OCUnit的测试用例类中是不是需要在公共接口中定义测试方法

如何在多个 OCUnit 测试用例之间共享代码?

为啥我的班级用错误代码 138 使我的 OCUnit 测试用例崩溃?

Ocunit - 创建单例测试类以在类之间共享代码