Reader small image

You're reading from  Test-Driven iOS Development with Swift - Fourth Edition

Product typeBook
Published inApr 2022
PublisherPackt
ISBN-139781803232485
Edition4th Edition
Right arrow
Author (1)
Dr. Dominik Hauser
Dr. Dominik Hauser
author image
Dr. Dominik Hauser

Dr. Dominik Hauser is an iOS developer working for a small company in western Germany. In over 11 years as an iOS developer, he has worked on many different apps, both large and small. In his spare time, Dominik builds small (often silly) apps and explores how he can become a better iOS developer. He talks at conferences, writes books, and is active in the iOS community in general. His most successful open source component (in terms of stars) is a pull-to-refresh control for a table view that consists of an actual SpriteKit game. Before Dominik became an iOS developer, he was a physicist, researching the most powerful photon sources within our galaxy.
Read more about Dr. Dominik Hauser

Right arrow

Chapter 11: Easy Navigation with Coordinators

An iOS app is usually a collection of single screens somehow connected to each other. Inexperienced developers often present a view controller from another view controller, because this is easy to implement and it is often shown that way in tutorials and demo code. But, for apps that need to be maintained over a long period of time, we need a pattern that is easier to understand and easier to change.

The coordinator pattern is very easy to implement and still manages to decouple the navigation between views of the app from the presentation of the information. In the coordinator pattern, a structure called a coordinator is responsible for navigating between views. View controllers tell the coordinator that the user interacted with the app and the coordinator decides which view controller should become responsible for the screen next.

As a bonus, the coordinator pattern makes testing navigation code simpler and more robust, and as a...

Technical requirement

Testing the app's setup

When our app starts, a coordinator should be instantiated and started. This should result in the presentation of the initial view of our app. Follow these steps to refactor the setup from using a storyboard to using a coordinator:

  1. Before we can refactor the setup of the app, we need a test that tells us when we break something. Select the ToDoTests group in the project navigator and add a new Unit Test Case Class instance with the name AppSetupTests.
  2. Replace the content of the new class with the following:
    // AppSetupTests.swift
    import XCTest
    @testable import ToDo
    class AppSetupTests: XCTestCase {
      func test_application_shouldSetupRoot() {
        let application = UIApplication.shared
        let scene = application.connectedScenes.first
        as? UIWindowScene
        let root =
          scene?.windows.first?.rootViewController
      ...

Adding missing parts

First, let's run the app for the first time to see where we are.

The app starts with a blank screen with just one plus (+) button in the upper-right corner.

Figure 11.2 – The initial view of our app

So, there is work to do here. But, let's move on and tap the plus (+) button. We are presented with the input view. We can add data for the item and tap the Save button.

Figure 11.3 – The input view of our app

But, when we tap the Save button, nothing happens. Dismiss the view by swiping down, and see whether the item was added. Something changed. There is a blank table view cell visible in the middle of the screen.

Figure 11.4 – A blank table view cell. Where is the to-do item?

When you tap the blank table view cell, the detail view is pushed onto the screen.

Figure 11.5 – The details of the to-do item. But, where is the due date...

Summary

In this final chapter, we have implemented the navigation between the different views of our app. We have learned how to test pushing view controllers onto a navigation stack and how we can test whether a view got presented modally.

With navigation implemented, we started the app on the simulator and found and fixed bugs. We figured out that TDD even helps when fixing bugs. By writing first a failing test for that bug and then making the test pass, we ensured that this bug won't hurt us in the future of our app.

With the skills you gained in this chapter, you will be able to implement and test the navigation of an app using the coordinator pattern. And, you are now able to write tests for bugs and fix the bug by making the test pass.

Congratulations, you reached the end of this book! My hope is that this book is the beginning of your journey to becoming a test-driven developer. You learned how to test Combine code and write tests for view controllers and views...

Exercises

  1. Fix the bugs you found while testing the app on the simulator.
  2. Add the feature that the user can check to-do items in the list of all to-do items.
  3. Write a review for this book on Amazon.

Why subscribe?

  • Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
  • Improve your learning with Skill Plans built especially for you
  • Get a free eBook or video every month
  • Fully searchable for easy access to vital information
  • Copy and paste, print, and bookmark content

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details.

At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Test-Driven iOS Development with Swift - Fourth Edition
Published in: Apr 2022Publisher: PacktISBN-13: 9781803232485
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Dr. Dominik Hauser

Dr. Dominik Hauser is an iOS developer working for a small company in western Germany. In over 11 years as an iOS developer, he has worked on many different apps, both large and small. In his spare time, Dominik builds small (often silly) apps and explores how he can become a better iOS developer. He talks at conferences, writes books, and is active in the iOS community in general. His most successful open source component (in terms of stars) is a pull-to-refresh control for a table view that consists of an actual SpriteKit game. Before Dominik became an iOS developer, he was a physicist, researching the most powerful photon sources within our galaxy.
Read more about Dr. Dominik Hauser