简体   繁体   English

未调用单元测试情节提要板viewDidLoad

[英]Unit Testing Storyboard viewDidLoad not called

So there are a few articles floating about on how to load a Storyboard containing a nib for unit testing and it looks something like this 因此,有几篇关于如何加载包含笔尖的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];
}

However when i do it the viewDidLoad method of the class is never called and i have to do it manually like so 但是,当我这样做时,永远不会调用该类的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];
}

As you can see i have to add [SUT viewDidLoad]; 如您所见,我必须添加[SUT viewDidLoad]; for viewDidLoad of the controller to be called at all...It works, and i should not worry too much since it's a suite of tests but i have misgivings about it since you should rarely have to call it manually, and shouldn't either instantiateViewControllerWithIdentifier: or the selector to call loadView make sure viewDidLoad is called? 完全可以调用该控制器的viewDidLoad ...它可以工作,我不应该太担心,因为它是一组测试,但是我对此感到担忧,因为您很少需要手动调用它,也不应该instantiateViewControllerWithIdentifier:或调用loadView的选择器确保viewDidLoad被调用?

UPDATE UPDATE

After a bit of experimentation it seems that only the UIViewController which has been set to be your storyboards initial UIViewController has it's viewDidLoad method called on instantiateViewControllerWithIdentifier: 一个实验位后,它似乎只有UIViewController已经设置为你的故事板最初UIViewController有它viewDidLoad呼吁方法instantiateViewControllerWithIdentifier:

I've recently come across the same situation as you and found my solution. 我最近遇到了与您相同的情况,并找到了解决方案。 You are correct viewDidLoad() is not called automatically. 您是正确的,不会自动调用viewDidLoad()。 In fact, you can call the view property of the view controller to trigger viewDidLoad(). 实际上,您可以调用视图控制器的view属性来触发viewDidLoad()。 For more information checkout out View Controller TDD 有关更多信息,请查看View Controller TDD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM