Modern SwiftUI: Dependencies & Testing, Part 2

Episode #220 • Jan 16, 2023 • Subscriber-Only

We conclude the series by taking control of the last two dependencies: persistence and speech recognition. We’ll make use of even more features of our new Dependencies library and we’ll write tests for our features that would have been impossible before.

Dependencies & Testing, Part 2
Introduction
00:05
Data manager dependency
00:35
Speech client dependency
25:30
Conclusion
39:11

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Introduction

We can make all of this much better if we finally take control over our dependency on the file system. In particular, the saving and loading of data to the file system.

Our Dependencies library does not come with such a client immediately available to us, but it is quite easy to create. This will give us a chance to show off how one registers a new dependency with the library so that it is immediately available everywhere via the @Dependency property wrapper.

This episode is for subscribers only.

Subscribe to Point-Free

Access this episode, plus all past and future episodes when you become a subscriber.

See plans and pricing

Already a subscriber? Log in

Exercises

  1. Use @Dependency(\.speechClient.authorizationStatus) in the standup detail view to show
    alert when authorization has been denied.

    Further, add an alert button action that uses @Dependency(\.openURL) to go to the
    application notification settings.

  2. Use @Dependency(\.date) and @Dependency(\.uuid) to take control of all instances of
    Date() and UUID() in the application.

  3. Write a test for the record screen that confirms that the timer pauses when the end meeting
    alert is presented. Use a test clock to show that when the alert is up, the elapsedSeconds
    state does not change when the clock is advanced. Dismiss the alert, advance the clock, and
    assert that the timer continues.

  4. Write a test for the deletion flow of a standup. Start the test off in a state with at least
    one standup, and simulate drilling down to the detail, tapping the delete button, assert
    that an alert was shown, simulate tapping the confirm deletion button, and then assert that
    the screen popped back to the root and the standup was removed from the collection.

  5. Let’s beef up the static DataManager.mock method to support accessing data from more than
    one URL in a test. Instead of wrapping a mutable blob of Data, wrap a [URL: Data]
    dictionary instead.

  6. Let’s further beef up the DataManager dependency. Define a static testValue that calls
    XCTFail by default. Then, define override methods that bypasses failure on a per-URL
    basis.

References

Getting started with Scrumdinger

Apple

Learn the essentials of iOS app development by building a fully functional app using SwiftUI.

Standups App

Brandon Williams & Stephen Celis

A rebuild of Apple’s “Scrumdinger” application that demosntrates how to build a complex, real world application that deals with many forms of navigation (e.g., sheets, drill-downs, alerts), many side effects (timers, speech recognizer, data persistence), and do so in a way that is testable and modular.

CasePaths

Brandon Williams & Stephen Celis • Monday Aug 23, 2021

Custom Dump is one of our open source projects. It provides a collection of tools for debugging, diffing, and testing your application’s data structures.

SwiftUI Navigation

Brandon Williams & Stephen Celis • Tuesday Sep 7, 2021

A library we open sourced. Tools for making SwiftUI navigation simpler, more ergonomic and more precise.

combine-schedulers

Brandon Williams & Stephen Celis • Sunday Jun 14, 2020

An open source library that provides schedulers for making Combine more testable and more versatile.

CasePaths

Brandon Williams & Stephen Celis

CasePaths is one of our open source projects for bringing the power and ergonomics of key paths to enums.

Clocks

Brandon Williams & Stephen Celis • Wednesday Jun 29, 2022

An open source library of ours. A few clocks that make working with Swift concurrency more testable and more versatile.

Dependencies

Brandon Williams & Stephen Celis • Sunday Jan 9, 2022

An open source library of ours. A dependency management library inspired by SwiftUI’s “environment.”

Identified Collections

Brandon Williams & Stephen Celis • Sunday Jul 11, 2021

Identified Collections is our open source library that provides an ergonomic, performant way to manage collections of identifiable data, and fits in perfectly with SwiftUI.

Tagged

Brandon Williams & Stephen Celis • Monday Apr 16, 2018

Tagged is one of our open source projects for expressing a way to distinguish otherwise indistinguishable types at compile time.

XCTest Dynamic Overlay

Brandon Williams & Stephen Celis • Wednesday Mar 17, 2021

XCTest Dynamic Overlay is a library we wrote that lets you write test helpers directly in your application and library code.

Packages authored by Point-Free

Swift Package Index

These packages are available as a package collection, usable in Xcode 13 or the Swift Package Manager 5.5.

Downloads