swift-protocol-di-testing

Implement protocol-based dependency injection for testable Swift code with Swift Testing.

Updated Mar 18, 2026
One-click install
npx skills add https://github.com/freedom909/real-estate-saas --skill swift-protocol-di-testing-freedom909
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: swift-protocol-di-testing
Source: https://github.com/freedom909/real-estate-saas/tree/main/.trae/skills/swift-protocol-di-testing
Command: npx skills add https://github.com/freedom909/real-estate-saas --skill swift-protocol-di-testing-freedom909

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Swift code that directly touches the file system, network, or external APIs is hard to test deterministically. This Skill provides patterns for abstracting those dependencies behind small, focused protocols so tests can run without real I/O and can simulate failure paths on demand. ## Core Features & Use Cases - Focused Protocol Design: Define single-responsibility protocols such as FileSystemProviding, FileAccessorProviding, and BookmarkStorageProviding, each covering one external concern. - Mock Implementations: Build mocks with configurable error properties to test error handling paths that are difficult to trigger in real environments. - Default Parameter Injection: Production code uses real implementations by default while tests inject mocks, with Sendable conformance for actor-based concurrency. - Use Case: When building an actor-based SyncManager that reads from an iCloud container, inject a MockFileAccessor to verify that missing containers and corrupt files throw the expected errors using Swift Testing's #expect macros. ## Quick Start Show me how to make my Swift SyncManager testable by abstracting its file system access behind protocols with mock implementations for Swift Testing.

Frequently Asked Questions about swift-protocol-di-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I make Swift code testable with dependency injection?▼

Define small protocols for each external dependency like file system or network access, then inject them via initializer default parameters. Production code uses real implementations by default while tests pass mock implementations with configurable behavior.

How to mock file system access in Swift unit tests?▼

Create a protocol such as FileAccessorProviding with read, write, and fileExists methods, then implement a mock backed by an in-memory dictionary of URLs to Data. Add error properties to simulate read and write failures for testing error handling paths.

Does protocol-based mocking work with Swift actors and Sendable?▼

Yes, protocols used across actor boundaries must conform to Sendable. Mock classes with mutable state can use @unchecked Sendable, while production structs conform naturally since they hold no mutable state.

What is the difference between Swift Testing and XCTest for DI tests?▼

Swift Testing uses @Test attributes and #expect macros instead of XCTestCase subclasses and XCTAssert functions. The dependency injection pattern works identically with both frameworks since mocks are injected through initializers.

When should I avoid creating protocols for Swift types?▼

Avoid protocols for types with no external dependencies, since mocking internal pure logic adds unnecessary indirection. Only abstract boundaries like file system, network, and external APIs, and never create a single large protocol covering all external access.