As I heard before and recently learned in Jon Reid's very good screencast, some init
methods or in iOS applications, the viewDidLoad
methods tend to get bigger and bigger. I tried to refactor this method:
- (void)viewDidLoad {
// 10+ lines of setting default property
// 20+ lines of setting up the navigation bar
// 20+ lines of resetting the view hierarchy
}
This was replaced by a very nice and short method that contains calls to other methods with speaking names:
- (void)viewDidLoad {
[super viewDidLoad];
[self setDefaultResourceNames];
[self setupRightBarButtonItems];
[self restoreNavigationControllerHierarchy];
[[NSNotificationCenter defaultCenter] addObserver: (...)];
}
Now those three methods are now being unit tested, which is much better than before. Now my question is, should the viewDidLoad
method be tested as well?
To do this, I made a partial mock out of my class under test and wrote the following test:
- (void)testViewDidLoadShouldInstantiateControllerCorrectly {
NewsItemDetailsViewController *sut = [[NewsItemDetailsViewController alloc] init];
id mockSut = [OCMockObject partialMockForObject:sut];
[[mockSut expect] setDefaultResourceNames];
[[mockSut expect] setupRightBarButtonItems];
[[mockSut expect] restoreNavigationControllerHierarchy];
[mockSut viewDidLoad];
[mockSut verify];
}
Is this any good? This seems to be largely coupled to actual source code and method execution rather than effects caused by calling the method (which is actually what unit testing is about, as far as I learned). But the effects of calling the method are actually covered in the three unit tests, testing the sub-methods.
Is it good to have this test or is it unnecessary when all other calls are already under test?