Benjamin Toueg Benjamin Toueg - 10 days ago 8
iOS Question

UnitTests and iOs : viewDidLoad triggered twice

I am testing the viewDidLoad event on one of my UIViewController.

- (void)testMyView
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
MyViewController *vc = [storyboard instantiateViewControllerWithIdentifier:MYID];
[vc viewDidLoad];

STAssertNotNil(vc, @"MyViewController should not be nil");
}


If I remove the line
[vc viewDidLoad];
, the
viewDidLoad
is never triggered.

If I let
[vc viewDidLoad];
in place, the
viewDidLoad
is triggered twice.

I understand that views are lazy loaded, but how can I avoid that behavior?

Is there any best practice regarding View testing?

Answer

You need to access the view in order to have it load automatically.

You can use something like this to do it without side effects:

vc.view.hidden = NO; // Or YES if it is supposed to be hidden.

Oh, and then remove your manual call to viewDidLoad as it won't be needed.