对于比赛我发展,我有触发通知时,他们的状态发生变化的几个模型类。然后,该视图订阅这些通知,并可以对它们做出反应。的OCUnit测试NSNotification交付
我做与的OCUnit模型我的单元测试,并希望断言预期的通知被张贴。对于这一点,我在做这样的事情:
- (void)testSomething {
[[NSNotificationCenter defaultCenter] addObserver:notifications selector:@selector(addObject:) name:kNotificationMoved object:board];
Board *board = [[Board alloc] init];
Tile *tile = [Tile newTile];
[board addTile:tile];
[board move:tile];
STAssertEquals((NSUInteger)1, [notifications count], nil);
// Assert the contents of the userInfo as well here
[board release];
}
的想法是,将NSNotificationCenter
通过调用其addObject:
方法通知添加到NSMutableArray
。
然而,当我运行它时,我发现addObject:
正在发送到某个其他对象(而不是我的NSMutableArray
),导致OCUnit停止工作。但是,如果我注释掉某些代码(例如release
调用,或添加新的单元测试),则所有事情都按预期开始工作。
我假设这有邻与时机的问题,或NSNotificationCenter
依靠以某种方式运行循环。
有没有任何建议来测试这个?我知道我可以在Board
中添加一个setter,并注入我自己的NSNotificationCenter
,但我正在寻找一种更快的方法来做到这一点(也许有一些关于如何动态替换NSNotificationCenter
的技巧)。
+1为单元测试通知的聪明方式! – 2011-07-21 13:53:43