All writing

Faking a shared data store in a UI-test launch

Most apps that inject their dependencies end up with a lot of small clients in front of one database - one for reading items, one for lists, one for stats, one for groups, and so on - and when the app launches under UI tests, each of those clients gets swapped for an in-memory fake, so every test starts from the same fixtures and nothing leaks into the next run. I ran into a bug in that setup in an iOS app of mine (SwiftUI, with Point-Free’s swift-dependencies doing the injection), and the lesson in it is less about Swift and more about where we put the fake.

A group that never showed up

The app has a list of items that can be gathered into groups, and the list shows a toolbar toggle once it holds at least one group. The UI test for it long-presses an item, picks “Add to Group”, creates a new group and then waits for the toggle. Every step passed in the activity log, the create form closed without an error, and the toggle never appeared within 60 seconds.

The obvious suspect is the condition that shows the toggle (it was also the code I had just changed), but the failure screenshot showed one plain item and no group at all, so there was nothing for the condition to see. The launch code explained it. It installed in-memory fakes for the item clients, but the group clients weren’t on that list, so they kept their live values and wrote to the real database:

let store = InMemoryItemStore()
prepareDependencies { store.configureDependencies(&$0) }
// the group clients were never mentioned, so they stayed live

The group got saved to the real database with our fixture item as its only member, but that item only existed in memory, and the list reads groups through the item rows, which never got a group id. Each half worked fine on its own, they just weren’t looking at the same data, and as a bonus every UI-test run had been writing groups into the simulator’s persisted store. The launch even had a guard that traps if it ends up “part live, part in-memory”, but it only checked the keys the item store installs, and the group keys were never among them.

The fix, and why it wasn’t enough

The fix was to install the existing in-memory group fake as well, but linked to the item fake rather than standing next to it. On its own the group fake keeps its own copies of member items, which would have just moved the split from “database vs memory” to “memory vs other memory”. Linked, it looks members up in the item store, and after every write that can change membership it pushes the whole membership map back onto the item rows:

let store = InMemoryItemStore()
let groupStore = InMemoryGroupStore(items: store)
prepareDependencies {
    store.configureDependencies(&$0)
    groupStore.configureDependencies(&$0)
}

That made the UI tests pass, and it’s worth giving such a fake unit tests of its own for the rules the real repository follows (a created group shows up on its items’ rows, removing a member clears its row, removing the last member deletes the group), since we now depend on it. After that, a walk through the same flows by hand on the simulator, with the same launch, turned up the same bug somewhere else. Marking a collection private hid nothing, because the collection list client was in memory but the code that answers “is this item private?” read through the live collection repository, so collections made in the app were invisible to it. No UI test marks a collection private, so nothing had caught it.

Two in a row is a pattern, and the linked group fake shows why. In the real database, an item’s group membership is a relationship on the item row, so the moment an item joins a group, every query that reads that item sees it, for free. With one fake per client, that consistency has to be rebuilt by hand for every pair of clients that share data, the group fake needs to know about the item fake, the privacy lookup would need to know about the collection fake, and every new client is one more place to forget. The guard can’t help either, because it can only check the keys someone already remembered to list.

Faking the store instead

What the real app has, and the fakes don’t, is a single store that every client is built on. So the fake should be at that level too - one in-memory store holding all the rows, and every client built from it the same way the live clients are built from the database:

// a sketch, not the app's code
final class InMemoryAppStore {
    private let state = Mutex(Rows())   // items, groups, collections, ...

    func install(_ values: inout DependencyValues) {
        values.itemClient = .make(reading: state)
        values.groupClient = .make(reading: state)
        values.collectionPrivacyClient = .make(reading: state)
        // every key that touches the data, in one place
    }
}

With that shape, a group created through one client is visible to every other client, because there’s only one place for it to be, and the privacy lookup can’t miss a collection for the same reason. There’s no membership map to push back and keep in sync, and the list of keys to install lives in exactly one function, which is also the natural place for a test that checks every data-backed dependency key is covered. The rules the repository enforces, like deleting a group once its last member is gone, still have to be written once in the fake, and the fake still needs tests against them, since a UI test can only be as right as the store underneath it.

Summary

If a test launch swaps dependencies for fakes one client at a time, it’s easy to end up with fakes that each work but don’t agree with each other, and the failure is silent, a step that “succeeds” with nothing on screen and no error. The thing to fake is the store the clients share, not the clients, so that one write is visible everywhere, the same as in production. When a UI test does something that should show up and doesn’t, it’s worth checking that the write and the read go to the same place before suspecting the product code.

Email address copied [email protected]