简体   繁体   中英

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

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

- (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]; 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?

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:

I've recently come across the same situation as you and found my solution. You are correct viewDidLoad() is not called automatically. In fact, you can call the view property of the view controller to trigger viewDidLoad(). For more information checkout out View Controller TDD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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