在我的基础模拟类中:
- (void)tearDown
{
_mockApplication = nil;
self.observerMock = nil;
self.notificationCenterMock = nil;
}
其中 notificaitonCenterMock 只是一个 id;
然后在我的测试中我做这样的事情:
self.notificationCenterMock = [OCMockObject partialMockForObject:[NSNotificationCenter defaultCenter]];
[(NSNotificationCenter *) [self.notificationCenterMock expect]
removeObserver:self.component
name:UIApplicationDidBecomeActiveNotification
object:nil];
现在.. 如果我运行此代码,我的单元测试将虚假地失败(即一次运行 370 个中只有 60 个将运行,下一次运行 70 或 65 个)。我的几个单元测试将失败并出现以下错误:
OCPartialMockObject[NSNotificationCenter]: expected method was not invoked: removeObserver:
<VPBCAdComponent-0x17d43e0-384381847.515513: 0x17d43e0> name:@"UIApplicationDidBecomeActiveNotification" object:nil
Unknown.m:0: error: -[VPBCAdComponentTests testCleanUpAfterDisplayingClickthrough_adBrowser_delegateCallback] :
OCPartialMockObject[NSNotificationCenter]: expected method was not invoked: removeObserver:
<VPBCAdComponent-0x17d43e0-384381847.515513: 0x17d43e0> name:@"UIApplicationDidBecomeActiveNotification" object:nil
然后将终止测试。我可以清楚地看到,部分模拟通知中心会导致运行测试套件出现问题。
问题是,我该怎么办?确保设置诸如观察者之类的重要事物以及回归证明会非常好。