© Gio Lodi 2021
G. LodiTest-Driven Development in Swifthttps://doi.org/10.1007/978-1-4842-7002-8_10

10. Testing Network Code

Gio Lodi1  
(1)
Mount Martha, VIC, Australia
 

How do you write tests for code interfacing with the network?

By sidestepping it with a Stub, to simulate different behaviors and avoid flaky tests.

A common problem faced when trying to test networking code is that the network is unpredictable and slow, resulting in non-deterministic tests — they may pass one time and fail the next.

In this chapter, we’ll see how to use the Dependency Inversion Principle and Stub Test Double techniques to decouple our code from the network’s real-world constraints.

The remote menu loading feature is almost complete. We built a view capable of updating ...

Get Test-Driven Development in Swift: Compile Better Code with XCTest and TDD now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.