[英]Unit Testing Storyboard viewDidLoad not called
因此,有几篇关于如何加载包含笔尖的Storyboard进行单元测试的文章,看起来就像这样
MyViewController vc;
- (void)setUp
{
[super setUp];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
self.vc = [storyboard instantiateViewControllerWithIdentifier:@"main"];
[self.vc performSelectorOnMainThread:@selector(loadView) withObject:nil waitUntilDone:YES];
}
但是,当我这样做时,永远不会调用该类的viewDidLoad
方法,因此我必须像这样手动进行
- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MediaSelectionSuite" bundle:[NSBundle mainBundle]];
SUT = [storyboard instantiateViewControllerWithIdentifier:@"Gallery"];
[SUT performSelectorOnMainThread:NSSelectorFromString(@"loadView") withObject:nil waitUntilDone:YES];
[SUT viewDidLoad];
}
如您所见,我必须添加[SUT viewDidLoad];
完全可以调用该控制器的viewDidLoad
...它可以工作,我不应该太担心,因为它是一组测试,但是我对此感到担忧,因为您很少需要手动调用它,也不应该instantiateViewControllerWithIdentifier:
或调用loadView
的选择器确保viewDidLoad
被调用?
UPDATE
一个实验位后,它似乎只有UIViewController
已经设置为你的故事板最初UIViewController
有它viewDidLoad
呼吁方法instantiateViewControllerWithIdentifier:
我最近遇到了与您相同的情况,并找到了解决方案。 您是正确的,不会自动调用viewDidLoad()。 实际上,您可以调用视图控制器的view属性来触发viewDidLoad()。 有关更多信息,请查看View Controller TDD
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.