Chapters

Hide chapters

iOS Test-Driven Development by Tutorials

Second Edition · iOS 15 · Swift 5.5 · Xcode 13

9. Using the Network Client
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

In the last chapter, you identified that ListingsViewController isn’t actually doing any networking. Rather, it has a // TODO comment in refreshData(). In response, you created DogPatchClient to handle networking logic. However, you haven’t used it yet.

In this chapter, you’ll update ListingsViewController to use DogPatchClient upon refreshing! Specifically, you will:

  • Add a shared instance on DogPatchClient.
  • Add a network client property on ListingsViewController.
  • Create a network client protocol.
  • Create a mock network client using the protocol.
  • Use the mock to stub and validate behavior.

Getting started

Feel free to use your project from the last chapter. If you want a fresh start, navigate to this chapter’s starter directory, open the DogPatch subdirectory and then open DogPatch.xcodeproj.

Once your project is ready, it’s time to jump in and set DogPatchClient up for networking by adding a shared instance.

Creating a shared instance

While you could instantiate DogPatchClient directly, this has disadvantages:

Adding a network client property

Next, you need to add a networkClient property to ListingsViewController. Before you can write app code, of course, you need a failing test.

Using the network client

While you could use DogPatchClient directly in your unit tests, this has several drawbacks:

Creating the network client protocol

What should you put in the network client protocol? Any methods and properties that callers need to use! In turn, you’ll be able to use your mock to validate that you’re calling these correctly.

Creating the mock network client

You next need to create the mock network client. Your first step is to write a test for… Oh, wait! You don’t need a test. ;]

Using the mock network client

You’re finally ready to use the mock network client!

Key points

In this chapter, you learned how to TDD using a network client. Here are the key points you covered:

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