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

13. Testing a Conditional View Presentation

Gio Lodi1  
(1)
Mount Martha, VIC, Australia
 

How do you test conditional logic to present a view when its code needs to live in the SwiftUI layer?

By extracting  as much of it as possible in the ViewModel layer and keep the SwiftUI implementation thin.

Sometimes, presenting a view is straightforward, such as loading the detail screen when selecting an item from a list. Other times, there is logic involved, like when the user should see a different alert based on an operation’s result.

Whenever there’s logic involved in presenting a view, using tests to drive its implementation is the best way to make sure it’s ...

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.