Test-Driven iOS Development With Swift - Sample Chapter
Test-Driven iOS Development With Swift - Sample Chapter
$ 39.99 US
25.99 UK
P U B L I S H I N G
Test-Driven iOS
Development with Swift
ee
pl
C o m m u n i t y
E x p e r i e n c e
D i s t i l l e d
Test-Driven iOS
Development with Swift
Create fully-featured and highly functional iOS apps by writing
tests first
Sa
m
Preface
iOS projects have become bigger and more complex. Many projects have already
surpassed desktop applications in their complexity. One important strategy to
manage this complexity is through the use of unit tests. By writing tests, a developer
can point out the intention of the code and provide a safety net against the
introduction of bugs.
By writing the tests first (Test-Driven Development), the developer focuses on the
problem. This way, they are forced to think about the domain and rephrase a feature
request using their own understanding by writing the test. In addition to this,
applications written using Test-Driven Development (TDD) only contain code that is
needed to solve the problem.
As a result, the code is clearer, and the developer gains more confidence that the
code actually works.
In this book, you will develop an entire iOS app using TDD. You will experience
different strategies of writing tests for models, View Controller, and networking code.
Preface
Chapter 5, Testing Network Code, teaches you to test network code using stubs to fake a
server component before it is developed.
Chapter 6, Putting It All Together, walks you through the integration of all the different
parts developed in previous chapters and shows the use of functional tests.
Chapter 7, Code Coverage and Continuous Integration, shows you how to measure the
code coverage of your tests using Xcode and introduces you to continuous integration.
Chapter 8, Where to Go from Here, wraps up and shows you the possible next steps to
improve your acquired testing skills.
[ 33 ]
Structure of an app
[ 34 ]
Chapter 2
User stories:
As a user, I want to see the list of to-do items when I open the app
In a to-do list app, the user will obviously need to be able to check items when
they are finished. The checked items are shown below the unchecked items, and
it is possible to uncheck them again. The app uses the delete button in the UI of
UITableView to check and uncheck items. Checked items will be put at the end of the
list in a section with the Finished header. The user can also delete all the items from
the list by tapping the trash button. The UI for the to-do item list will look like this:
User stories:
As a user, I want to see all the checked items below the unchecked items
When the user taps an entry, the details of this entry is shown in the task detail view.
[ 35 ]
User stories:
As a user, given that I have tapped a to-do item in the list, I want to see its
details
Chapter 2
User stories:
As a user, given that I have tapped the add (+) button in the item list, I want
to see a form to put in the details (title, optional date, optional location name,
optional address, and optional description) of a to-do item
As a user, I want to add a to-do item to the list of to-do items by tapping on
the Save button
We will not implement the editing and deletion of tasks. But when you have worked
through this book completely, it will be easy for you to add this feature yourself by
writing the tests first.
Keep in mind that we will not test the look and design of the app. Unit tests cannot
figure out if an app looks like it was intended. Unit tests can test features, and these
are independent of their presentation. In principle, it would be possible to write
unit tests for the position and color of UI elements. But such things are very likely to
change a lot in the early stages of development. We do not want to have failing tests
only because a button has moved 10 points.
However, we will test whether the UI elements are present on the view. If your
user cannot see the information for the tasks, or if it is not possible to add all the
information of a task, then the app does not meet the requirements.
[ 37 ]
[ 38 ]
Chapter 2
A model
The model of the application consists of the to-do item, the location, and an item
manager, which allows the addition and removal of items and is also responsible for
managing the items. Therefore, the controller will ask the item manager for the items
to present. The item manager will also be responsible for storing the items on disc.
Beginners often tend to manage the model objects within the controller. Then, the
controller has a reference to a collection of items, and the addition and removal
of items is directly done by the controller. This is not recommended because if we
decide to change the storage of the items (for example, by using Core Data), their
addition and removal would have to be changed within the controller. It is difficult
to keep an overview of such a class, and because of this reason, it is a source of bugs.
It is much easier to have a clear interface between the controller and the model
objects because if we need to change how the model objects are managed, the
controller can stay the same. We could even replace the complete model layer if we
just keep the interface the same. Later in the chapter, we will see that this decoupling
also helps to make testing easier.
[ 39 ]
Development strategy
In this book, we will build the app from inside out. We will start with the model,
and then build the controllers and networking. At the end of the book, we will put
everything together.
Of course, this is not the only way to build apps. But by separating on the basis
of layers instead of features, it is easier to follow and keep an overview of what is
happening. When you later need to refresh your memory, the relevant information
you need is easier to find.
[ 40 ]
Chapter 2
To take a look at how the application target and test target fit together, select the
project in Project Navigator, and then select the ToDoTests target. In the General
tab, you'll find a setting for the host application that the test target should be able to
test. It should look like this:
Xcode has already set up the test target correctly to allow the testing of the
implementations that we will write in the application target.
Xcode has also set up a scheme to build the app and run the tests. Click on the Scheme
selector next to the stop button in the toolbar, and select Edit Scheme.... In the Test
action, all the test bundles of the project will be listed. In our case, only one test bundle
is shown: ToDoTests. On the right-hand side of the shown window is a column
named Test, with a checked checkbox. This means that if we run the tests while this
scheme is selected in Xcode, all the tests in the selected test suite will be run.
[ 41 ]
[ 42 ]
Chapter 2
Testing behaviors
To write code, I have an Xcode tab called Coding. Usually, in this tab, the test is open
on the left-hand side, and in the Assistant Editor on the right-hand side is the code
to be tested (or in the case of TDD, the code to be written). It looks like this:
When the test starts, we want to see the code editor again. So, we add a behavior
to show the Coding tab. In addition to this, we want to see the Test Navigator and
debugger with the console view.
When the test succeeds, Xcode should show a bezel to notify us that all tests have
passed. Go to the Testing | Succeeds stage, and check the Notify using bezel or
system notification setting. In addition to this, it should hide the navigator and the
debugger because we want to concentrate on refactoring or writing the next test.
[ 43 ]
In case testing fails (which happens a lot in TDD), Xcode should show a bezel again.
I like to hide the debugger because usually it is not the best place to figure out what
is going on in the case of a failing test. And in TDD, in most cases, we already know
what the problem is. But we want to see the failing test. Therefore, check Show
navigator and select Issue navigator. At the bottom of the window, check Navigate
to and select first new issue.
You can even make your Mac speak the announcements. Check Speak
announcements using and select the voice you like. But be careful not to annoy
your coworkers. You might need their help in the future.
Now, the project and Xcode are set up, and we can start our TDD journey.
Summary
In this chapter, we took a look at the app we are going to build throughout the
course of this book. We took a look at how the screens of the app will look when we
are finished. We created the project that we will use later on, and we learned about
Xcode behaviors.
In the next chapter, we will develop the data model of the app using TDD. We will
use structs for the model wherever we can because models are best represented in
Swift by value types. We will add some conformance to the Equatable protocol to
make the comparison of the model instances easier.
[ 44 ]
www.PacktPub.com
Stay Connected: