我不确定如何在单元测试中执行segue,以便prepareForSegue将运行。
尽我所能覆盖单元测试的所有内容,这是我正在尝试的:
- (void)setUp {
[super setUp];
_mainVC = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]
instantiateViewControllerWithIdentifier:kMainVC];
_detailsVC = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]
instantiateViewControllerWithIdentifier:kDetailsVC];
}
- (void)test_prepareForSegue_will_set_confNumber_on_DetailsVC {
UIStoryboardSegue *segue = [UIStoryboardSegue segueWithIdentifier:kDetailsSegueId source:_mainVC destination:_detailsVC performHandler:^{
// do anything here ???
}];
[segue perform];
expect(_detailsVC.textFieldStr).to.equal(@"123456abc");
}
我注意到当我插入一个断点时,主视图控制器中的prepareForSegue从未被命中。
最佳答案
DUH。
只需在被测试的ViewController上调用prepareForSegue :)
UIStoryboardSegue *segue = [UIStoryboardSegue segueWithIdentifier:kDetailsSegueId source:_mainVC destination:_detailsVC performHandler:^{
// do nothing here
}];
[_mainVC prepareForSegue:segue sender:self];
expect(_detailsVC.confNumber).to.equal(@"123456abc");
关于ios - 测试prepareForSegue是否执行了应做的工作的方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17842030/