I’m looking for a “best practice” / “low test friction” way to do state based testing on view controllers inside my base AppDelegate class. Currently the below provides an easy way to stub in my own UIViewController (using ocmock) when something happens to it inside a method on the class.
-(FirstViewController *)getFirstViewController
{
if (self.viewController1)
{ return self.viewController1; }
self.viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
return self.viewController1;
}
The first question I have -Is this a valid way to stub out / inject my own mock view controller for testing? (seems to work great but I’m not sure if this is how the pros are doing state based testing today)
The next question I have -Is it valid to keep 1 copy of the view controller in memory like this (only creating it from scratch once for the life of the app) ?
**note- I would dependency inject this but my init is already large enough just injecting the nav controller and tab bar controller so that’s not an option for this large class sadly
If it’s the root view controller, you should just make it a property of your app delegate:
Unless the method you’re testing is the method where you initialize
firstViewController, you don’t need any kind of lazy loading approach. You just get the app delegate in your test, create an instance ofFirstViewControllerand assign it to the property on your delegate, and define the test:If you want to mock out the controller for whatever you’re testing, you can do that as well: