Swift Protocol Di Testing

基于协议的依赖注入(Protocol-based Dependency Injection),通过精简的协议和 Swift 测试框架(Swift Testing)来模拟文件系统、网络和外部 API,编写可测试的 Swift 代码。

2,037 stars 332 forksMobile Development Plugin marketplace

Install

Claude Code plugin
/plugin marketplace add xu-xiang/everything-claude-code-zh

The repo is a Claude Code plugin marketplace. Run /plugin afterwards to pick the plugin that includes this skill.

skills CLI (Claude Code, Cursor, Codex and more)
npx skills add xu-xiang/everything-claude-code-zh --skill swift-protocol-di-testing
Manual install
git clone --depth 1 https://github.com/xu-xiang/everything-claude-code-zh.git /tmp/everything-claude-code-zh
mkdir -p ~/.claude/skills
cp -r /tmp/everything-claude-code-zh/skills/swift-protocol-di-testing ~/.claude/skills/swift-protocol-di-testing

Personal skills live in ~/.claude/skills. Use .claude/skills in a project folder to share a skill with your team.

From the SKILL.md

# 基于 Swift 协议的依赖注入(Testing)

通过将外部依赖(文件系统、网络、iCloud)抽象到精简且功能集中的协议(Protocols)后面,使 Swift 代码变得可测试。这种模式支持无需实际 I/O 即可进行确定性测试(Deterministic Tests)。

## 何时激活

- 编写访问文件系统、网络或外部 API 的 Swift 代码时
- 需要在不触发实际故障的情况下测试错误处理路径时
- 构建需要在不同环境(App、测试、SwiftUI 预览)下运行的模块时
- 使用 Swift 并发(Concurrency,如 Actor、Sendable)设计可测试架构时

## 核心模式

### 1. 定义精简且功能集中的协议

每个协议应仅处理一个特定的外部关注点。

```swift
// 文件系统访问
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
}

// 书签存储(例如:用于沙盒应用)
public protocol BookmarkStorageProviding: Sendable {
    func saveBookmark(_ data: Data, for key: String) throws
    func loadBookmark(for key: String) throws -> Data?
}
```

### 2. 创建默认(生产环境)实现
Read the full skill on GitHub

Details

Skill name
swift-protocol-di-testing
Path
skills/swift-protocol-di-testing/SKILL.md
License
MIT
Repo updated
5 March 2026

FAQ

What does the Swift Protocol Di Testing skill do?

基于协议的依赖注入(Protocol-based Dependency Injection),通过精简的协议和 Swift 测试框架(Swift Testing)来模拟文件系统、网络和外部 API,编写可测试的 Swift 代码。

How do I install the Swift Protocol Di Testing skill?

Claude Code plugin: /plugin marketplace add xu-xiang/everything-claude-code-zh. Skills go in ~/.claude/skills (personal) or .claude/skills (project), and Claude loads them automatically when a task matches the description.

Is Swift Protocol Di Testing popular?

It lives in xu-xiang/everything-claude-code-zh, which has 2,037 GitHub stars and 332 forks.

Related skills

Browse the most popular Claude skills, all mobile development skills, or write your own with the skills generator.