Shared State: Testing, Part 1

Episode #271 • Mar 18, 2024 • Subscriber-Only

The @Shared property wrapper can effortlessly share state among features to build complex flows quickly, but because it is powered by a reference type, it is not compatible with the Composable Architecture’s value-oriented testing tools. Let’s address these shortcomings and recover all of the library’s testing niceties.

Previous episode
Shared State: Testing, Part 1
Next episode
Locked

Unlock This Episode

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

Sign in with GitHub

Introduction

Brandon

And so this is absolutely incredible. We now have the basics of dedicated @Shared property wrapper that allows us to easily share data between multiple features. And we’ve seen very concretely that this allows us to create complex features quite easily.

Now in doing this we did have to come face-to-face with what it means to put a reference type into our state. On the one hand it’s really no different than using a dependency to model shared state. Dependencies are very reference-like, even if they are modeled as structs, and so we used that fact to justify using a reference type directly in state. And thanks to Swift’s observation tools, reference types are now observable, and so everything just worked really nicely.

Stephen

However, what is not going to be so nice about everything we have done so far is testing. Reference types are notoriously difficult to test because they are an amalgamation of data and behavior, and because they can’t be copied. This makes it difficult to compare the data inside a reference before and after a mutation has occurred so that we can assert on how it changed in an easy and exhaustive manner.

Let’s see these problems concretely, and then see how we might fix them.

The problem with testing


Downloads

Get started with our free plan

Our free plan includes 1 subscriber-only episode of your choice, access to 62 free episodes with transcripts and code samples, and weekly updates from our newsletter.

View plans and pricing