swift-protocol-di-testing

Implements protocol-based dependency injection for testable Swift code with mockable external dependencies.

Updated Mar 25, 2026
One-click install
npx skills add https://github.com/Femad-6/my-skills --skill swift-protocol-di-testing-femad-6
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: swift-protocol-di-testing
Source: https://github.com/Femad-6/my-skills/tree/main/.github/skills/swift-protocol-di-testing
Command: npx skills add https://github.com/Femad-6/my-skills --skill swift-protocol-di-testing-femad-6

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, making error paths and edge cases nearly impossible to verify without real I/O. ## Core Features & Use Cases - Focused Protocol Design: Define small, single-responsibility protocols (file system, file access, bookmark storage) with Sendable conformance for actor-safe concurrency. - Mock Implementations: Build configurable mocks with in-memory storage and injectable error properties to simulate failure paths like missing files or read errors. - Swift Testing Integration: Write deterministic tests using Swift Testing's #expect macros to verify both success and error handling behavior. - Use Case: When building a SyncManager actor that reads from an iCloud container, inject a MockFileAccessor that throws CocoaError to verify the sync logic handles read failures gracefully without touching disk. ## Quick Start Refactor my Swift SyncManager to use protocol-based dependency injection and write Swift Testing tests with mocked file system errors.

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 (file system, network), create production implementations, and inject them via initializer default parameters. Tests then pass mock implementations with configurable state and error properties.

How to mock file system access in Swift tests?

Create a mock class conforming to your FileAccessorProviding protocol that stores files in an in-memory dictionary keyed by URL. Add optional error properties like readError and writeError to simulate failure paths deterministically.

Does protocol-based DI work with Swift actors and Sendable?

Yes, protocols should declare Sendable conformance when used across actor boundaries. Mock classes with mutable state can use @unchecked Sendable, while struct-based production implementations conform naturally.

When should I not use protocol-based dependency injection in Swift?

Avoid it for types with no external dependencies, since mocking internal types adds complexity without benefit. Also avoid creating one large protocol covering all external access; keep each protocol focused on a single concern.

How do I test error handling paths in Swift without real failures?

Design mocks with configurable error properties, then set them before invoking the code under test. Use Swift Testing's #expect(throws:) macro to assert the expected error type is thrown.