As I heard and recently found out in Jon Reid a very good screencast , some init methods or in iOS applications, viewDidLoad methods are getting bigger and bigger. I tried to reorganize 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 has been replaced by a very beautiful and short method that contains calls to other methods with the names of the speakers:
- (void)viewDidLoad { [super viewDidLoad]; [self setDefaultResourceNames]; [self setupRightBarButtonItems]; [self restoreNavigationControllerHierarchy]; [[NSNotificationCenter defaultCenter] addObserver: (...)]; }
Now these three methods are now tested per unit, which is much better than before. My question now is whether the viewDidLoad method should be checked?
To do this, I made a partial layout of my test class 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]; }
It's good? This seems to be pretty much related to the actual source code and the execution of the method, and not to the effects caused by the method call (actually this is about unit testing, as far as I know). But the effects of method invocation are actually considered in three unit tests, testing sub-methods.
Is it good to have this test or not necessary when all other calls are already being tested?
source share