Test a method that has been reorganized (divided into several methods)?

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?

+4
source share
2 answers

This is a perfectly reasonable approach. You want to test two types of things: 1) the things you don’t want to break, and 2) the things you want to document. If this approach is suitable for any of these criteria or for both of these criteria, you will be fine.

I think the main drawback of such tests is that they can add a lot of coding overhead to for granted. Viewing the installation code is often very readable - you can scan it and understand what is happening, so the tests do not add a lot of documentation. And if the view is the one you use every time you use the application, it can be immediately seen when you perform QA, if any of the viewing settings are broken, so the tests do not add a lot of coding protection value.

In the end, you are the one who will maintain this code and be responsible for fixing it when it breaks, so you decide whether testing will outweigh the risks more later.

+1
source

It depends on what you are testing. There is no general solution; You need to make a decision based on your own professional judgment.

Is it useful in this case to test the control flow or provide it with a system of protection against regression? Are benefits greater than costs?

Mockery for the sake of consistency is largely discouraged. In most cases, it was harmful. Use it as a tool where you really need it.

+1
source

All Articles