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

12. Testing Side Effects

Gio Lodi1  
(1)
Mount Martha, VIC, Australia
 

How do you write tests for a system that does not return an output but changes another object’s state instead?

By spying on the object affected by the SUT.

If you have control over the object on which the side effect occurs, you can inspect its state to verify it matches your expectation, but what if you don’t? For example, how can you verify that an object calls a method on a third-party SDK as a result of a particular operation?

This chapter introduces a new kind of Test Double that allows us to do precisely that: the Spy. Using a Spy, you can replace a real dependency of your SUT ...

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.