Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
affaan-m avatar

Swift Protocol Di Testing

  • 5.6k installs
  • 238k repo stars
  • Updated August 5, 2026
  • affaan-m/everything-claude-code

swift-protocol-di-testing is an agent skill that Protocol-based dependency injection for testable Swift code - mock file system, network, and external APIs using focused protocols and Swift Testing.

About

Protocol-based dependency injection for testable Swift code mock file system network and external APIs using focused protocols and Swift Testing Any Swift code that touches file system network or external APIs Testing error handling paths that are hard to trigger in real environments Building modules that need to work in app test and SwiftUI preview contexts Apps using Swift concurrency actors structured concurrency that need 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 han 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

  • Swift Protocol-Based Dependency Injection for Testing
  • 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)

Swift Protocol Di Testing by the numbers

  • 5,636 all-time installs (skills.sh)
  • +221 installs in the week ending Aug 5, 2026 (Skillselion tracking)
  • Ranked #264 of 2,153 Testing & QA skills by installs in the Skillselion catalog
  • Security screen: MEDIUM risk (skills.sh audit)
  • Data as of Aug 5, 2026 (Skillselion catalog sync)
At a glance

swift-protocol-di-testing capabilities & compatibility

Capabilities
swift protocol based dependency injection for te · writing swift code that accesses file system, ne · need to test error handling paths without trigge · building modules that work across environments ( · designing testable architecture with swift concu
Use cases
copywriting · api development · devops · testing
From the docs

What swift-protocol-di-testing says it does

# 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.
SKILL.md
Define Small, Focused Protocols Each protocol handles exactly one external concern.
SKILL.md
Create Default (Production) Implementations ```swift public struct DefaultFileSystemProvider: FileSystemProviding { public init() {} public func containerURL(for purpose: Purpose) -> URL?
SKILL.md
npx skills add https://github.com/affaan-m/everything-claude-code --skill swift-protocol-di-testing

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs5.6k
repo stars238k
Security audit3 / 3 scanners passed
Last updatedAugust 5, 2026
Repositoryaffaan-m/everything-claude-code

What problem does swift-protocol-di-testing solve for developers working with swift-protocol-di-testing?

Protocol-based dependency injection for testable Swift code - mock file system, network, and external APIs using focused protocols and Swift Testing.

Who is it for?

Developers using swift-protocol-di-testing for tasks described in the skill documentation.

Skip if: Skip when docs are unavailable or the task is outside swift-protocol-di-testing's documented scope.

When should I use this skill?

Protocol-based dependency injection for testable Swift code - mock file system, network, and external APIs using focused protocols and Swift Testing.

What you get

Structured workflows and grounded outputs from SKILL.md when invoking swift-protocol-di-testing.

  • Protocol interfaces for I/O dependencies
  • Mock implementations and Swift Testing test files

Files

SKILL.mdMarkdownGitHub ↗

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
}

// Bookmark storage (e.g., for sandboxed apps)
public protocol BookmarkStorageProviding: Sendable {
    func saveBookmark(_ data: Data, for key: String) throws
    func loadBookmark(for key: String) throws -> Data?
}

2. Create Default (Production) Implementations

public struct DefaultFileSystemProvider: FileSystemProviding {
    public init() {}

    public func containerURL(for purpose: Purpose) -> URL? {
        FileManager.default.url(forUbiquityContainerIdentifier: nil)
    }
}

public struct DefaultFileAccessor: FileAccessorProviding {
    public init() {}

    public func read(from url: URL) throws -> Data {
        try Data(contentsOf: url)
    }

    public func write(_ data: Data, to url: URL) throws {
        try data.write(to: url, options: .atomic)
    }

    public func fileExists(at url: URL) -> Bool {
        FileManager.default.fileExists(atPath: url.path)
    }
}

3. Create Mock Implementations for Testing

/// NOTE: Not thread-safe. Use only in single-threaded test contexts.
public final class MockFileAccessor: FileAccessorProviding, @unchecked Sendable {
    public var files: [URL: Data] = [:]
    public var readError: Error?
    public var writeError: Error?

    public init() {}

    public func read(from url: URL) throws -> Data {
        if let error = readError { throw error }
        guard let data = files[url] else {
            throw CocoaError(.fileReadNoSuchFile)
        }
        return data
    }

    public func write(_ data: Data, to url: URL) throws {
        if let error = writeError { throw error }
        files[url] = data
    }

    public func fileExists(at url: URL) -> Bool {
        files[url] != nil
    }
}

4. Inject Dependencies with Default Parameters

Production code uses defaults; tests inject mocks.

public actor SyncManager {
    private let fileSystem: FileSystemProviding
    private let fileAccessor: FileAccessorProviding

    public init(
        fileSystem: FileSystemProviding = DefaultFileSystemProvider(),
        fileAccessor: FileAccessorProviding = DefaultFileAccessor()
    ) {
        self.fileSystem = fileSystem
        self.fileAccessor = fileAccessor
    }

    public func sync() async throws {
        guard let containerURL = fileSystem.containerURL(for: .sync) else {
            throw SyncError.containerNotAvailable
        }
        let data = try fileAccessor.read(
            from: containerURL.appendingPathComponent("data.json")
        )
        // Process data...
    }
}

5. Write Tests with Swift Testing

import Testing

@Test("Sync manager handles missing container")
func testMissingContainer() async {
    let mockFileSystem = MockFileSystemProvider(containerURL: nil)
    let manager = SyncManager(fileSystem: mockFileSystem)

    await #expect(throws: SyncError.containerNotAvailable) {
        try await manager.sync()
    }
}

@Test("Sync manager reads data correctly")
func testReadData() async throws {
    let mockFileAccessor = MockFileAccessor()
    mockFileAccessor.files[testURL] = testData

    let manager = SyncManager(fileAccessor: mockFileAccessor)
    let result = try await manager.loadData()

    #expect(result == expectedData)
}

@Test("Sync manager handles read errors gracefully")
func testReadError() async {
    let mockFileAccessor = MockFileAccessor()
    mockFileAccessor.readError = CocoaError(.fileReadCorruptFile)

    let manager = SyncManager(fileAccessor: mockFileAccessor)

    await #expect(throws: SyncError.self) {
        try await manager.sync()
    }
}

Best Practices

  • Single Responsibility: Each protocol should handle one concern — don't create "god protocols" with many methods
  • Sendable conformance: Required when protocols are used across actor boundaries
  • Default parameters: Let production code use real implementations by default; only tests need to specify mocks
  • Error simulation: Design mocks with configurable error properties for testing failure paths
  • Only mock boundaries: Mock external dependencies (file system, network, APIs), not internal types

Anti-Patterns to Avoid

  • Creating a single large protocol that covers all external access
  • Mocking internal types that have no external dependencies
  • Using #if DEBUG conditionals instead of proper dependency injection
  • Forgetting Sendable conformance when used with actors
  • Over-engineering: if a type has no external dependencies, it doesn't need a protocol

When to Use

  • Any Swift code that touches file system, network, or external APIs
  • Testing error handling paths that are hard to trigger in real environments
  • Building modules that need to work in app, test, and SwiftUI preview contexts
  • Apps using Swift concurrency (actors, structured concurrency) that need testable architecture

Related skills

Forks & variants (1)

Swift Protocol Di Testing has 1 known copy in the catalog totaling 1.4k installs. They canonicalize to this original listing.

How it compares

Pick swift-protocol-di-testing over singleton service locators when Swift modules need swappable I/O boundaries for tests and previews.

FAQ

What does swift-protocol-di-testing do?

Protocol-based dependency injection for testable Swift code - mock file system, network, and external APIs using focused protocols and Swift Testing.

When should I invoke swift-protocol-di-testing?

Protocol-based dependency injection for testable Swift code - mock file system, network, and external APIs using focused protocols and Swift Testing.

Is Swift Protocol Di Testing safe to install?

skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.