Count

The requirements of Chapter 2, Planning and Structuring Your Test-Driven iOS App, ask for a list with unchecked to-do items at the top and checked to-do items at the bottom of the list in the app. How the items are presented is not a matter of concern with regard to the model. But it has to be possible to get the number of unchecked and checked to-do items from the item manager.

Add the following code to ItemManagerTests:

func test_ToDoCount_Initially_IsZero() { 
  let sut = ItemManager() 
} 

The sut abbreviation stands for System Under Test. We could also write this as itemManager, but using sut makes it easier to read, and it also allows us to copy and paste test code into other tests when appropriate.

The test is not yet finished, but it already fails because ItemManager is an unresolved identifier. Open Project Navigator again and select the ToDo group. Go to iOS | Source | Swift File. This will create a Swift file; let's call it ItemManager.swift, and select the Model folder as the file location.

Add the following class definition:

class ItemManager {
}

This is enough to make the test code compilable. Run the tests to make sure that all the tests pass and we can continue writing tests. In test_ToDoCount_Initially_IsZero(), add the assert function highlighted in the following code:

func test_ToDoCount_Initially_IsZero() { 
  let sut = ItemManager() 
   

  XCTAssertEqual(sut.toDoCount, 0) 
} 

With this addition, the test method tests whether ItemManager has the toDoCount property and whether it is initially set to zero.

The test still does not compile again because Value of type 'ItemManager' has no member 'toDoCount'. The simplest way to make the test pass is to add the following property declaration to ItemManager:

let toDoCount = 0 

Run the tests. All the tests pass. The code and tests look good, so we do not need to refactor them.

In addition to the unchecked items, we also need to be able to get the number of checked items from the item manager. Add the following test to ItemManagerTests:

func test_DoneCount_Initially_IsZero() { 
  let sut = ItemManager() 
  
 
  XCTAssertEqual(sut.doneCount, 0) 
} 

To make this test pass, add the following property definition to ItemManager:

let doneCount = 0 

Run the tests to check that this is enough to make them pass. If we look at the previously written test methods, we'll see a repetition. The sut variable is initialized in each test method. Let's refactor the test methods and remove the repetition. Add the following property declaration to the beginning of ItemManagerTests:

var sut: ItemManager! 

Then, at the end of setUp(), add this initialization of sut:

sut = ItemManager()

Add the line sut = nil in tearDown() right before super.tearDown(). Now, we can remove the initialization from the tests:

func test_ToDoCount_Initially_IsZero() { 
  XCTAssertEqual(sut.toDoCount, 0) 
} 
 
func test_DoneCount_Initially_IsZero() { XCTAssertEqual(sut.doneCount, 0) }

Run the tests again to make sure that we have not broken anything with the refactoring.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.191.214