swift-protocol-di-testing

基于协议的依赖注入,用于可测试的Swift代码——使用聚焦协议和Swift Testing模拟文件系统、网络和外部API。

144,923 stars
Complexity: easy

About this skill

This skill describes a robust software development pattern for Swift, leveraging protocol-based Dependency Injection (DI) to enhance code testability and maintainability. It advocates for abstracting external dependencies—such as file systems, network interactions, and third-party APIs (e.g., iCloud)—into small, highly focused protocols. By doing so, concrete implementations can be easily swapped out with mock objects during testing, enabling deterministic tests that do not rely on actual I/O operations or external service availability. The pattern promotes building modules that are adaptable across different environments (application, testing, SwiftUI previews) and supports modern Swift concurrency features like actors and Sendable types, ensuring a resilient and production-ready architecture. This approach is particularly valuable for AI agents tasked with generating high-quality Swift code, as it guides them to produce code that inherently follows best practices for testability and architectural flexibility.

Best use case

Generating well-structured, easily testable Swift code for applications that interact with external systems or require robust error handling and modularity.

基于协议的依赖注入,用于可测试的Swift代码——使用聚焦协议和Swift Testing模拟文件系统、网络和外部API。

Highly testable, modular, and maintainable Swift code with clear separation of concerns. The generated code will facilitate easier unit testing, mocking, and adaptation to different environments, adhering to modern Swift best practices for concurrency and dependency management.

Practical example

Example input

Generate a Swift module for managing user settings that are persisted to the file system. Ensure the module is highly testable using protocol-based dependency injection. Include interfaces for file system access and basic read/write operations, and demonstrate how to test it.

Example output

// MARK: - Protocols for Dependency Injection

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

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
    func removeItem(at url: URL) throws
}

// Concrete implementations (for production)
public struct RealFileSystemProvider: FileSystemProviding {
    public func containerURL(for purpose: Purpose) -> URL? {
        // ... actual implementation using FileManager ...
        return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.example.app")
    }
}

public struct RealFileAccessorProvider: FileAccessorProviding {
    public func read(from url: URL) throws -> Data {
        return try Data(contentsOf: url)
    }
    public func write(_ data: Data, to url: URL) throws {
        try data.write(to: url, options: .atomicWrite)
    }
    public func fileExists(at url: URL) -> Bool {
        return FileManager.default.fileExists(atPath: url.path)
    }
    public func removeItem(at url: URL) throws {
        try FileManager.default.removeItem(at: url)
    }
}

// MARK: - Core Logic

public enum Purpose: String, Sendable {
    case settings
    case cache
}

public actor UserSettingsManager {
    private let fileSystemProvider: FileSystemProviding
    private let fileAccessor: FileAccessorProviding
    private let settingsFileName: String = "user_settings.json"

    public init(fileSystemProvider: FileSystemProviding, fileAccessor: FileAccessorProviding) {
        self.fileSystemProvider = fileSystemProvider
        self.fileAccessor = fileAccessor
    }

    private func settingsURL() throws -> URL {
        guard let containerURL = fileSystemProvider.containerURL(for: .settings) else {
            throw SettingsError.containerNotFound
        }
        return containerURL.appendingPathComponent(settingsFileName)
    }

    public func loadSettings<T: Decodable>() throws -> T {
        let url = try settingsURL()
        if !fileAccessor.fileExists(at: url) {
            throw SettingsError.notFound
        }
        let data = try fileAccessor.read(from: url)
        return try JSONDecoder().decode(T.self, from: data)
    }

    public func saveSettings<T: Encodable>(_ settings: T) throws {
        let url = try settingsURL()
        let data = try JSONEncoder().encode(settings)
        try fileAccessor.write(data, to: url)
    }

    enum SettingsError: Error {
        case containerNotFound
        case notFound
        case encodingFailed
        case decodingFailed
    }
}

// MARK: - Example Usage in Tests

import XCTest
// import SwiftTesting // Use if integrating with Swift Testing framework

final class UserSettingsManagerTests: XCTestCase {

    // Mock implementations for testing
    class MockFileSystemProvider: FileSystemProviding {
        var mockContainerURL: URL?
        func containerURL(for purpose: Purpose) -> URL? {
            return mockContainerURL
        }
    }

    class MockFileAccessorProvider: FileAccessorProviding {
        var mockFileData: Data? // Data to return on read
        var fileExistsMap: [URL: Bool] = [:] // Simulate file existence
        var writtenData: [URL: Data] = [:] // Capture data written

        func read(from url: URL) throws -> Data {
            if fileExistsMap[url] == false { throw NSError(domain: "TestError", code: 404, userInfo: nil) }
            return mockFileData ?? Data()
        }
        func write(_ data: Data, to url: URL) throws {
            writtenData[url] = data
            fileExistsMap[url] = true
        }
        func fileExists(at url: URL) -> Bool {
            return fileExistsMap[url] ?? false
        }
        func removeItem(at url: URL) throws {
            writtenData.removeValue(forKey: url)
            fileExistsMap.removeValue(forKey: url)
        }
    }

    func testLoadSettingsSuccess() async throws {
        let mockContainerURL = URL(fileURLWithPath: "/mock/container")
        let mockSettingsURL = mockContainerURL.appendingPathComponent("user_settings.json")
        let mockSettingsData = #"{"username": "testuser"}"#.data(using: .utf8)!

        let mockFileSystem = MockFileSystemProvider()
        mockFileSystem.mockContainerURL = mockContainerURL

        let mockFileAccessor = MockFileAccessorProvider()
        mockFileAccessor.mockFileData = mockSettingsData
        mockFileAccessor.fileExistsMap[mockSettingsURL] = true

        let manager = UserSettingsManager(fileSystemProvider: mockFileSystem, fileAccessor: mockFileAccessor)

        struct Settings: Codable, Equatable { let username: String }
        let loadedSettings: Settings = try await manager.loadSettings()
        XCTAssertEqual(loadedSettings, Settings(username: "testuser"))
    }

    func testSaveSettings() async throws {
        let mockContainerURL = URL(fileURLWithPath: "/mock/container")
        let mockSettingsURL = mockContainerURL.appendingPathComponent("user_settings.json")

        let mockFileSystem = MockFileSystemProvider()
        mockFileSystem.mockContainerURL = mockContainerURL

        let mockFileAccessor = MockFileAccessorProvider()

        let manager = UserSettingsManager(fileSystemProvider: mockFileSystem, fileAccessor: mockFileAccessor)

        struct Settings: Codable { let username: String }
        let settingsToSave = Settings(username: "newuser")
        try await manager.saveSettings(settingsToSave)

        XCTAssertNotNil(mockFileAccessor.writtenData[mockSettingsURL])
        let savedData = try XCTUnwrap(mockFileAccessor.writtenData[mockSettingsURL])
        let decodedSavedSettings = try JSONDecoder().decode(Settings.self, from: savedData)
        XCTAssertEqual(decodedSavedSettings.username, "newuser")
    }
}

When to use this skill

  • When writing Swift code that interacts with file systems, network services, or external APIs.
  • When needing to test error handling paths without triggering real system failures.
  • When building modular Swift components designed to work across various environments (e.g., main app, unit tests, SwiftUI previews).
  • When designing testable architectures that support Swift concurrency features like `actor` and `Sendable`.

When not to use this skill

  • For very simple Swift scripts or components that have no external dependencies and no significant need for extensive unit testing.
  • When the overhead of defining protocols and managing dependencies outweighs the benefits for extremely small, self-contained functions where testability is not a primary concern.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/swift-protocol-di-testing/SKILL.md --create-dirs "https://raw.githubusercontent.com/affaan-m/everything-claude-code/main/docs/zh-CN/skills/swift-protocol-di-testing/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/swift-protocol-di-testing/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How swift-protocol-di-testing Compares

Feature / Agentswift-protocol-di-testingStandard Approach
Platform SupportClaudeLimited / Varies
Context Awareness High Baseline
Installation ComplexityeasyN/A

Frequently Asked Questions

What does this skill do?

基于协议的依赖注入,用于可测试的Swift代码——使用聚焦协议和Swift Testing模拟文件系统、网络和外部API。

Which AI agents support this skill?

This skill is designed for Claude.

How difficult is it to install?

The installation complexity is rated as easy. You can find the installation instructions above.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

Related Guides

SKILL.md Source

# 基于协议的 Swift 依赖注入测试

通过将外部依赖(文件系统、网络、iCloud)抽象为小型、专注的协议,使 Swift 代码可测试的模式。支持无需 I/O 的确定性测试。

## 何时激活

* 编写访问文件系统、网络或外部 API 的 Swift 代码时
* 需要在未触发真实故障的情况下测试错误处理路径时
* 构建需要在不同环境(应用、测试、SwiftUI 预览)中工作的模块时
* 设计支持 Swift 并发(actor、Sendable)的可测试架构时

## 核心模式

### 1. 定义小型、专注的协议

每个协议仅处理一个外部关注点。

```swift
// 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. 创建默认(生产)实现

```swift
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. 创建用于测试的模拟实现

```swift
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. 使用默认参数注入依赖项

生产代码使用默认值;测试注入模拟对象。

```swift
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. 使用 Swift Testing 编写测试

```swift
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()
    }
}
```

## 最佳实践

* **单一职责**:每个协议应处理一个关注点——不要创建包含许多方法的“上帝协议”
* **Sendable 一致性**:当协议跨 actor 边界使用时需要
* **默认参数**:让生产代码默认使用真实实现;只有测试需要指定模拟对象
* **错误模拟**:设计具有可配置错误属性的模拟对象以测试故障路径
* **仅模拟边界**:模拟外部依赖(文件系统、网络、API),而非内部类型

## 需要避免的反模式

* 创建覆盖所有外部访问的单个大型协议
* 模拟没有外部依赖的内部类型
* 使用 `#if DEBUG` 条件语句代替适当的依赖注入
* 与 actor 一起使用时忘记 `Sendable` 一致性
* 过度设计:如果一个类型没有外部依赖,则不需要协议

## 何时使用

* 任何触及文件系统、网络或外部 API 的 Swift 代码
* 测试在真实环境中难以触发的错误处理路径时
* 构建需要在应用、测试和 SwiftUI 预览上下文中工作的模块时
* 需要使用可测试架构的、采用 Swift 并发(actor、结构化并发)的应用

Related Skills

swiftui-patterns

144923
from affaan-m/everything-claude-code

SwiftUI 架构模式,使用 @Observable 进行状态管理,视图组合,导航,性能优化,以及现代 iOS/macOS UI 最佳实践。

DevelopmentClaude

swift-concurrency-6-2

144923
from affaan-m/everything-claude-code

Swift 6.2 可接近的并发性 — 默认单线程,@concurrent 用于显式后台卸载,隔离一致性用于主 actor 类型。

DevelopmentClaude

swift-actor-persistence

144923
from affaan-m/everything-claude-code

在 Swift 中使用 actor 实现线程安全的数据持久化——基于内存缓存与文件支持的存储,通过设计消除数据竞争。

DevelopmentClaude

perl-testing

144923
from affaan-m/everything-claude-code

使用Test2::V0、Test::More、prove runner、模拟、Devel::Cover覆盖率和TDD方法的Perl测试模式。

DevelopmentClaude

rust-testing

144923
from affaan-m/everything-claude-code

Rust testing patterns including unit tests, integration tests, async testing, property-based testing, mocking, and coverage. Follows TDD methodology.

DevelopmentClaude

kotlin-testing

144923
from affaan-m/everything-claude-code

Kotest, MockK, coroutine testi, property-based testing ve Kover coverage ile Kotlin test kalıpları. İdiomatic Kotlin uygulamalarıyla TDD metodolojisini takip eder.

DevelopmentClaude

cpp-testing

144923
from affaan-m/everything-claude-code

C++ テストの作成/更新/修正、GoogleTest/CTest の設定、失敗またはフレーキーなテストの診断、カバレッジ/サニタイザーの追加時にのみ使用します。

DevelopmentClaude

python-testing

144923
from affaan-m/everything-claude-code

Python testing best practices using pytest including fixtures, parametrization, mocking, coverage analysis, async testing, and test organization. Use when writing or improving Python tests.

DevelopmentClaude

golang-testing

144923
from affaan-m/everything-claude-code

Go testing best practices including table-driven tests, test helpers, benchmarking, race detection, coverage analysis, and integration testing patterns. Use when writing or improving Go tests.

DevelopmentClaude

workspace-surface-audit

144923
from affaan-m/everything-claude-code

Audit the active repo, MCP servers, plugins, connectors, env surfaces, and harness setup, then recommend the highest-value ECC-native skills, hooks, agents, and operator workflows. Use when the user wants help setting up Claude Code or understanding what capabilities are actually available in their environment.

DevelopmentClaude

safety-guard

144923
from affaan-m/everything-claude-code

Use this skill to prevent destructive operations when working on production systems or running agents autonomously.

DevelopmentClaude

repo-scan

144923
from affaan-m/everything-claude-code

Cross-stack source code asset audit — classifies every file, detects embedded third-party libraries, and delivers actionable four-level verdicts per module with interactive HTML reports.

DevelopmentClaude