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

6. Testing Static SwiftUI Views

Gio Lodi1  
(1)
Mount Martha, VIC, Australia
 

How do you use Test-Driven Development for the view layer when there is no straightforward way to test SwiftUI views?

By extracting all the view configuration logic in a dedicated object and test that instead.

Xcode Previews and manual testing are not the only ways to make sure your views behave correctly. In this chapter, we’ll learn how to use tests to guide us in implementing the view behavior. The key is to make the view humble and free from any logic and have it ask what to show to an object that is not tied to SwiftUI, one we can easily implement test-first. We’ll call this ...

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.