lundi 2 mars 2015

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 shouldent either instantiateViewControllerWithIdentifier: or the selector to call loadView make sure viewDidLoad is called?


UPDATE


After a bit of experimentation it seems that only the ViewController which has been set to be your storyboards initial view controller has it's viewDidLoad method called on instantiateViewControllerWithIdentifier:


Aucun commentaire:

Enregistrer un commentaire