Chapters

Hide chapters

iOS Test-Driven Development by Tutorials

First Edition · iOS 13 · Swift 5.1 · Xcode 11

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

8. RESTful Networking
Written by Joshua Greene

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

You’ll learn how to TDD a RESTful networking client in this chapter. Specifically, you will:

  • Set up the networking client.
  • Ensure the correct endpoint is called.
  • Handle networking errors, valid responses and invalid responses.
  • Dispatch results to a response queue.

Get excited! TDD networking awesomeness is coming your way.

Getting started

Navigate to the starter directory for this chapter, and you’ll find it has a DogPatch subdirectory containing DogPatch.xcodeproj. Open this project file in Xcode, and take a look.

You’ll see a few files have already been added for you. Here are the important ones for this chapter:

  • Controllers/ListingsViewController.swift contains the view controller that displays the fetched Dogs or Error.

  • Models/Dog.swift contains the Dog model that represents each pup.

You’ll also see an empty group for Networking. This contains the networking client and related types.

Build and run the app, and the following error-message screen will greet you:

If you pull down to refresh, the activity indicator will animate, but it will never finish.

Open ListingsViewController.swift, and you’ll see tableView(_:numberOfRowsInSection:) is hardcoded to return 1.

Within tableView(_:cellForRowAt:), it performs a check to see if viewModels.count is greater than zero. This will always be false because the app isn’t setting the viewModels. Rather, it needs to create these from a networking response.

However, there’a comment for // TODO: Write this within refreshData(), so the app isn’t making any network calls.

Your job is now clear – you need to write the logic to make networking calls! While you could write this as a one-off networking call directly within ListingsViewController, this view controller would quickly become very large.

A better option is to create a separate networking client that handles all of the networking logic – this is the focus of this chapter!

Setting up the networking client

Before you write any production code, you first need to write a failing test.

TDDing the networking call

You’ll need to make a GET request to fetch a list of Dog objects from the server. You’ll break this down into several smaller tasks:

Calling the right URL

You’ll start by ensuring that you call the right URL. Unfortunately, URLSession doesn’t actually have a way to check which URL was called. The easiest way to do this is by mocking URLSession through subclassing it. To prevent any actual networking calls from being made in your unit tests, you’ll also mock URLSessionDataTask.

Handling error responses

Your next task is to handle error responses. There are two scenarios that indicate an error occurred:

Deserializing Dog models

You’re finally ready to handle the happy-path case, handling a successful response.

Dispatching to a response queue

Your DogPatchClient is handling networking like a boss! There’s just one problem – you’ve been mocking URLSessionDataTask to prevent real networking calls from being made, but unfortunately, you’ve also masked a behavior of URLSessionDataTask.

Adding a response queue

Add the following test right after test_init_sets_session(), ignoring the compiler error for now:

Updating the mocks

You next need to update MockURLSession and MockURLSessionDataTask to call the completion handler on a dispatch queue. First, add this new property to MockURLSession:

Handling dispatch scenarios

You next need to verify that completionHandler is dispatched to the responseQueue. This should happen in these cases:

Key points

In this chapter, you learned how to do TDD for a networking client. Let’s recap what you learned:

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now