affaan-m avatar

Swift Protocol Di Testing

This teaches protocol-based dependency injection for Swift, the kind you actually need when testing file system operations, network calls, or iCloud sync without hitting real I/O. You define small, focused protocols for each external boundary (file reading, bookmark storage), inject them with default parameters so production code stays clean, and swap in mocks for tests. The pattern works well with actors and Sendable requirements. It's straightforward and avoids the common mistakes like creating giant protocols or mocking everything. If you're writing Swift code that needs deterministic tests for error paths or works across app, test, and preview contexts, this is the standard approach done right.

skills.sh installs
5K
GitHub stars
229.1K
last updated
Jul 14, 2026

Install

$ npx -y skills add affaan-m/everything-claude-code --skill swift-protocol-di-testing --agent claude-code

Installs into .claude/skills of the current project.

Files

Swift Protocol-Based Dependency Injection for Testing

Patterns for making Swift code testable by abstracting external dependencies (file system, network, iCloud) behind small, focused protocols. Enables deterministic tests without I/O.

When to Activate

  • Writing Swift code that accesses file system, network, or external APIs
  • Need to test error handling paths without triggering real failures
  • Building modules that work across environments (app, test, SwiftUI preview)
  • Designing testable architecture with Swift concurrency (actors, Sendable)

Core Pattern

1. Define Small, Focused Protocols

Each protocol handles exactly one external concern.

// File system access
public protocol FileSystemProviding: Sendable {
    func containerURL(for purpose: Purpose) -> URL?
}

// File read/write operations
public protocol FileAccessorProviding: Sendable {
    func read(from url: URL) throws -> Data
    func write(_ data: Data, to url: URL) throws
    func fileExists(at url: URL) -> Bool
}

Sponsors

Details

Repository
github.com/affaan-m/everything-claude-code
Categories
Testing & QA, Mobile Development

More from affaan-m/everything-claude-code

All 256
1trailofbits avatarmutation-testingtrailofbits/skillsConfigures mewt or muton mutation testing campaigns — scopes targets, tunes timeouts, and optimizes long-running runs. Use when the user mentions mewt, muton, m4.7Kskills.sh installs
2dotnet avatarcode-testing-extensionsdotnet/skillsProvides file paths to language-specific extension files for the code-testing pipeline. Call this skill to discover available extension guidance files (e.g., do4.2Kskills.sh installs
3everyinc avatarce-test-browsereveryinc/compound-engineering-pluginRun browser tests for pages affected by the current branch or PR.3.1Kskills.sh installs
4anthropics avatarsox-testinganthropics/knowledge-work-pluginsGenerate SOX sample selections, testing workpapers, and control assessments. Use when planning quarterly or annual SOX 404 testing, pulling a sample for a contr2.8Kskills.sh installs
5mastra-ai avatarsmoke-testmastra-ai/mastraCreate a Mastra project using create-mastra and smoke test the studio in Chrome using Chrome MCP server2.5Kskills.sh installs
6owl-listener avatara-b-test-designowl-listener/designer-skillsDesign rigorous A/B tests with hypotheses, variants, metrics, and sample size calculations.2.3Kskills.sh installs