我想测试某个方法被调用以响应收到的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次调用?
btw,如果我调试 - 调试器确实停在方法
答案 0 :(得分:0)
您应该将测试重新编写到下一个:
- (void)setUp {
_sut = [[EAMainScreenViewController alloc] <properInit>];
}
- (void)tearDown {
//just in case
[[NSNotificationCenter defaultCenter] removeObserver:_sut];
}
- (void)testThatNotificationPushReceived {
[_sut viewDidLoad];
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationPushReceived object:<probably some mock> userInfo:nil];
//test something that happens with your mock
}