Benjamin Toueg Benjamin Toueg - 1 year ago 79
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
is never triggered.

If I let
[vc viewDidLoad];
in place, the
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download