我想测试某个方法被调用以响应收到的 NSNotification
我试过这个:
_sut = mock([EAMainScreenViewController class]);
[_sut view];
[[NSNotificationCenter defaultCenter]postNotificationName:kNotificationPushReceived object:[UIApplication sharedApplication].delegate userInfo:nil];
[verify(_sut) pushReceived:anything()];
我的 _sut viewDidLoad 看起来像这样
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushReceived:) name:kNotificationPushReceived object:[UIApplication sharedApplication].delegate];
}
为什么我的测试在预期 1 次调用和接收 0 次调用时失败?
顺便说一句,如果我调试 - 调试器确实停止在该方法