在我的基础模拟类中:
- (void)tearDown
{
_mockApplication = nil;
self.observerMock = nil;
self.notificationCenterMock = nil;
}
其中notificaitonCenterMock只是一个标识符;
然后在我的测试中,我会这样做:
self.notificationCenterMock = [OCMockObject partialMockForObject:[NSNotificationCenter defaultCenter]];
[(NSNotificationCenter *) [self.notificationCenterMock expect]
removeObserver:self.component
name:UIApplicationDidBecomeActiveNotification
object:nil];
现在,如果我运行这段代码,我的单元测试将会出现错误(即一次只有60个测试用例中的370个会被运行,下一次则是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
测试将会被终止。我清楚地看到,部分模拟通知中心会导致运行测试套件时出现问题。
问题是,我该怎么办?确保设置了重要的观察者和回归证明将非常好。