我想创建一个集成测试,它表明某个动作会导致模态视图控制器的显示。故事板设置有 2 个视图控制器,一个具有自定义 ViewController 类,第二个具有默认 UIViewController 类和标题“second”。segue 设置为具有标识符“modalsegue”的模态。在模拟器中运行应用程序效果很好,但我在定义正确的测试时遇到了很多麻烦。
视图控制器.m:
@implementation ViewController
- (IBAction)handleActionByPerformingModalSegue {
[self performSegueWithIdentifier:@"modalsegue" sender:self];
}
@end
测试:
- (void)testActionCausesDisplayOfSecondViewController {
ViewController * vc =
[[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]
instantiateViewControllerWithIdentifier:@"ViewController"];
[vc handleActionByPerformingModalSegue];
STAssertEquals(vc.presentedViewController.title, @"second",
@"Title of presented view controller should be second but is %@",
vc.presentedViewController.title, nil);
}
在以下输出中运行测试结果:
2013-06-23 17:38:44.164 SeguesRUs[15291:c07] Warning: Attempt to present <UIViewController: 0x7561370> on <ViewController: 0x7566590> whose view is not in the window hierarchy!
SeguesRUsTests.m:33: error: -[SeguesRUsTests testActionCausesDisplayOfSecondViewController] : '<00000000>' should be equal to '<9c210d07>': Title of presented view controller should be second but is (null)
我究竟做错了什么?有没有一种简单的方法可以避免第一条消息?