swift-protocol-di-testing

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

1|Updated Oct 11, 2025
One-click install
npx skills add https://github.com/ibytechaos/claude --skill swift-protocol-di-testing-ibytechaos
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: swift-protocol-di-testing
Source: https://github.com/ibytechaos/claude/tree/main/plugins/everything-claude-code/skills/swift-protocol-di-testing
Command: npx skills add https://github.com/ibytechaos/claude --skill swift-protocol-di-testing-ibytechaos

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Swift code that directly accesses the file system, network, or external APIs is hard to test deterministically, making error-handling paths nearly impossible to verify without triggering real failures. ## Core Features & Use Cases - Focused Protocol Design: Define small, single-responsibility protocols (FileSystemProviding, FileAccessorProviding, BookmarkStorageProviding) that abstract each external concern. - Mock Implementations: Build configurable mocks with in-memory storage and injectable error properties to simulate failure paths without real I/O. - Swift Testing Integration: Write deterministic tests using Swift Testing (@Test, #expect) with actors and Sendable-safe concurrency. - Use Case: You have a SyncManager actor that reads from an iCloud container. Inject a MockFileAccessor that throws a read error, then verify the manager surfaces the correct SyncError without touching the disk. ## Quick Start Ask the AI to refactor a Swift class that reads files so its file system access sits behind an injectable protocol with a mock 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 (file system, network), provide production implementations as default parameter values, and inject mock implementations in tests. This lets tests run deterministically without real I/O.

How to mock file system access in Swift tests?

Create a mock conforming to your FileAccessorProviding protocol backed by an in-memory dictionary of URLs to Data. Add configurable error properties so tests can simulate read and write failures on demand.

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

Yes, but protocols used across actor boundaries must conform to Sendable. Reference-type mocks can use @unchecked Sendable when their mutable state is only accessed within tests.

Should I use one big protocol or many small protocols in Swift?

Use many small, single-responsibility protocols. A large protocol covering all external access is an anti-pattern that makes mocks bloated and couples unrelated concerns together.

When should I not create a protocol for a Swift type?

Skip protocols for types with no external dependencies. Only mock boundaries like file system, network, and APIs; mocking internal pure-logic types adds indirection without testing benefit.