본문으로 건너뛰기
cccskills
무료GitHub에 공개

swift-protocol-di-testing

Make Swift code that uses files, networks, or external APIs easier to test by placing small protocols around those boundaries. Inject production or mock implementations, then use Swift Testing to check successful results and failure paths without real I/O.

한국어 요약은 준비 중입니다. 영어 요약을 표시하고 있습니다.

원문원문 설명 보기

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

실행 점수 미검증

설치 방법 보기

이럴 때 유용합니다

  • Testing Swift code that reads or writes files
  • Simulating network or API failures
  • Building modules for apps, tests, and SwiftUI previews
  • Testing actor-based Swift concurrency code

포함된 파일(1)

  • SKILL.md5.7 KB

SKILL.md(원문)

설치하기 전에 에이전트에게 전달되는 지시 내용을 확인할 수 있습니다.

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

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

何时激活

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

核心模式

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

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

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

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

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

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

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 编写测试

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、结构化并发)的应用

리뷰

아직 리뷰가 없습니다. 사용해 본 소감을 남겨 주세요.

댓글

로그인 없이 쓸 수 있습니다. 사용 후기, 질문, 작동하지 않은 환경 등을 편하게 남겨 주세요.

아직 댓글이 없습니다. 첫 댓글을 남겨 보세요.

보내면 바로 공개됩니다. 이메일 주소나 전화번호 같은 개인 정보는 쓰지 마세요. URL은 2개까지입니다.

0 / 1,000자

이 양식은 reCAPTCHA로 보호되며 Google 개인정보처리방침 및 서비스 약관이 적용됩니다.

같은 저장소의 스킬

Use this skill to design or review REST APIs. It covers resource URLs, HTTP methods and status codes, pagination, filtering, sorting, error responses, authentication, rate limits, and versioning, with patterns for consistent endpoint contracts.

한국어 요약은 준비 중입니다. 영어 요약을 표시하고 있습니다.

  • Designing a new REST endpoint
  • Reviewing an existing API contract
  • Choosing pagination and filtering patterns
xu-xiang/everything-claude-code-zh2천2026년 3월 5일 업데이트

Use this skill to design or review REST APIs, including resource URLs, HTTP methods and status codes, pagination, filtering, errors, versioning, and rate limits. It helps make endpoint behavior and response formats consistent for clients and developers.

한국어 요약은 준비 중입니다. 영어 요약을 표시하고 있습니다.

  • Designing endpoints for a new REST API
  • Reviewing an API contract
  • Adding pagination, filtering, or sorting
xu-xiang/everything-claude-code-zh2천2026년 3월 5일 업데이트

Draft or refine long-form articles, guides, tutorials, and newsletters using a supplied style reference or a clear, practical default voice. The skill helps shape structure, improve pacing, and check claims against source material without inventing credentials or evidence.

한국어 요약은 준비 중입니다. 영어 요약을 표시하고 있습니다.

  • Turning research notes into a polished article
  • Matching a founder’s or brand’s writing voice
  • Improving the structure of a long draft
xu-xiang/everything-claude-code-zh2천2026년 3월 5일 업데이트

Draft or revise long-form pieces such as articles, guides, tutorials, and newsletters. The skill helps shape a clear structure, match a supplied writing sample or brand guide, and ground claims in provided sources without inventing biographical facts, company metrics, or testimonials.

한국어 요약은 준비 중입니다. 영어 요약을 표시하고 있습니다.

  • Drafting a blog post from notes or research
  • Matching a supplied brand voice
  • Restructuring a long draft for clarity
xu-xiang/everything-claude-code-zh2천2026년 3월 5일 업데이트

Use this skill to draft or revise long-form content such as articles, guides, tutorials, and newsletters. It can shape structure and pacing, turn notes or research into polished copy, and match a supplied voice sample or brand guide while checking claims against sources.

한국어 요약은 준비 중입니다. 영어 요약을 표시하고 있습니다.

  • Drafting a blog post or newsletter
  • Turning notes or research into an article
  • Matching a supplied writing sample
xu-xiang/everything-claude-code-zh2천2026년 3월 5일 업데이트

Draft or refine long-form writing such as articles, guides, blog posts, tutorials, and newsletters. It can follow supplied writing samples or brand guidance, organize sections around a clear purpose, and keep claims grounded in the provided sources.

한국어 요약은 준비 중입니다. 영어 요약을 표시하고 있습니다.

  • Draft a blog post from notes or research
  • Match an article to supplied writing samples
  • Turn a transcript into a polished guide
xu-xiang/everything-claude-code-zh2천2026년 3월 5일 업데이트

xu-xiang의 스킬 모두 보기

이 스킬의 문제 신고