Install any skill in seconds. Free to start, no credit card required.
Get Started Free →xu-xiang/swift-protocol-di-testing
基于协议的依赖注入(Protocol-based Dependency Injection),通过精简的协议和 Swift 测试框架(Swift Testing)来模拟文件系统、网络和外部 API,编写可测试的 Swift 代码。
Passed 21 of 22 test cases with the skill, and 18 of 22 without it.
and 1 more in the full report
4 fixed17 passed either way0 still failing1 broke
Tokens, turns and time describe resource use, not a dollar price.
One of 4 test cases the skill fixed. Same model, same prompt, with and without the skill.
我正在编写一个涉及本地文件读写的 Swift Service,但在写单元测试时无法方便地模拟文件缺失和读取错误的场景。请帮我重构这段代码,提供抽象接口定义、生产环境实现、能够手动注入错误的测试 Mock,以及基于 Swift Testing 框架的测试用例代码。
| Checked | Without skill | With skill |
|---|---|---|
| The protocol declaration explicitly inherits from `Sendable`. | Missed | Passed |
| The mock implementation uses explicit error properties to simulate read and write failures. | Passed | Passed |
| The test cases use the Swift Testing framework `@Test` attribute and `#expect(throws:)` assertions. | Passed | Passed |
The instructions your model reads when the skill loads.
通过将外部依赖(文件系统、网络、iCloud)抽象到精简且功能集中的协议(Protocols)后面,使 Swift 代码变得可测试。这种模式支持无需实际 I/O 即可进行确定性测试(Deterministic Tests)。
每个协议应仅处理一个特定的外部关注点。
// 文件系统访问
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?
}
test cases passed, vs 18 / 22 without
Published skills whose descriptions closely match this one, with their headline benchmark lift.
More skills from the same source and nearby in the registry, with their headline benchmark lift.