Refactor - example 1

Before writing more tests, we need to refactor the existing ones. In the production code, there is nothing to refactor. This code couldn't be simpler or more elegant. In the test case, we now have two test methods. Both start by creating an instance of ViewController. This is a repetition of code and a good candidate for refactoring.

Add the following property at the beginning of the FirstDemoTests class:

var viewController: ViewController! 

Remember that the setUp() method is called before each test is executed. So, it is the perfect place to initialize the viewController property:

override func setUp() { 
  super.setUp() 
  viewController = ViewController() 
} 

Now, we can remove this let viewController = ViewController() ...

Get Test-Driven iOS Development with Swift 4 - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.