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

7. Testing Dynamic SwiftUI Views

Gio Lodi1  
(1)
Mount Martha, VIC, Australia
 

How do you test the way a view updates when it receives new data?

By wrapping the updates in a @Published property of the ViewModel and test that using asynchronous expectations .

In the previous chapter, we saw that SwiftUI views are hard to test and that the trick is to bypass them, moving all of the presentation logic in a dedicated object, the ViewModel. When all your presentation logic is in the ViewModel, you can test dynamic views as well as static ones.

SwiftUI and Combine let you seamlessly update the view as new data becomes available. You can make the ViewModel publish ...

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.