Swift Actor in Unit Tests
Article Summary
Abdul Moiz from Thumbtack reveals how Swift 6's concurrency checks broke their entire test suite. Their solution? A clever protocol-based pattern that makes actors mockable without sacrificing thread safety.
When Thumbtack's iOS team upgraded to Xcode 15.3, Swift's new concurrency warnings forced them to adopt actors for thread safety. But actors can't be subclassed, breaking their dependency injection and unit testing framework. The team developed a generalized solution using associated types that works across their entire codebase.
Key Takeaways
- Swift actors provide automatic thread safety but can't be mocked in tests
- Protocol with associatedtype enables stubbing different actors with same interface
- Solution works with existing dependency injection without learning curve
- Approach scales to any actor without custom implementation per type
Critical Insight
Thumbtack created a reusable pattern using protocol associated types that lets teams adopt Swift actors while maintaining full unit test coverage.