Swift Protocol-Based Dependency Injection for Testing — portable workflow
Protocol-based dependency injection for testable Swift code — mock file system, network, and external APIs using focused protocols and Swift Testing. Use when Swift code needs testing and file system, network, or external APIs must be mocked.
Use it only when the request supplies this domain, framework, data source or artifact; a shared keyword is not a match. Related skills, each for its own concern: bosskuai-agent-architecture-audit, bosskuai-angular-developer, bosskuai-story-lifecycle.
Workflow
Read the source workflow first, then work through these sections in order:
- Core Pattern.
- Define Small, Focused Protocols.
- Create Default (Production) Implementations.
- Create Mock Implementations for Testing.
- Inject Dependencies with Default Parameters.
- Write Tests with Swift Testing.
- Resolve its
scripts/, references/, assets/ and template paths against references/upstream/, not the project root. Source skill names map through SOURCE.json (bossku skills show <id>). ECC slash commands, agent names and package-root paths are not installed: use what this host has, or say what is missing.
- Check the project's tool versions and the source prerequisites first: read
swift-tools-version in Package.swift, run xcodebuild -version and swift --version, and check whether the test targets import Testing or XCTest. Check the latest stable Swift release at swift.org/download at work time, and use only APIs present in the installed version. A documented command is an example, not permission to install packages, add an account, expose secrets, upload private content or change an external service.
Done when
Apply the source “Core Pattern”, “1. Define Small, Focused Protocols” guidance to the concrete request. Return the resulting swift protocol-based dependency injection for testing artifact or diagnosis with source/command evidence for each conclusion.
- Type traps: a mock must return the shapes production sends (a number as text, a missing key,
null). Feed it recorded, sanitised production responses, not only happy-path values.
- Concurrency: a mock that records calls must be safe under parallel tests. Use an actor or a lock, or the tests can flake.
- Zero downtime: change a protocol in steps. Add the new requirement to the protocol, with a default implementation in a protocol extension. Move the callers and the mock conformers to the new requirement. Then remove the extension default once every conformer implements it, so each step builds and ships.
- Full quality bar:
../../references/checklists/engineering-quality-bar.md.
If an input, provider, runtime or authorisation is missing, stop that step, carry on with read-only analysis, and say exactly what is missing. A generated plan alone is not completion.
Adapted from the locally reviewed affaan-m/ECC snapshot: provenance and hashes in SOURCE.json, the original text and resources in references/upstream/ under its MIT licence. Copyright (c) 2026 Affaan Mustafa.
1---2name: bosskuai-swift-protocol-di-testing3description: "Use when the task explicitly concerns 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. Use when Swift code needs testing and file system, network, or external APIs must be mocked."4metadata:5 source: https://skillmd.com/skills/wankimmy/bosskuai-swift-protocol-di-testing6 origin: ECC-adaptation7---89# Swift Protocol-Based Dependency Injection for Testing — portable workflow1011Protocol-based dependency injection for testable Swift code — mock file system, network, and external APIs using focused protocols and Swift Testing. Use when Swift code needs testing and file system, network, or external APIs must be mocked.1213Use it only when the request supplies this domain, framework, data source or artifact; a shared keyword is not a match. Related skills, each for its own concern: `bosskuai-agent-architecture-audit`, `bosskuai-angular-developer`, `bosskuai-story-lifecycle`.1415## Workflow1617Read [the source workflow](references/upstream/SKILL.md) first, then work through these sections in order:18191. Core Pattern.202. 1. Define Small, Focused Protocols.213. 2. Create Default (Production) Implementations.224. 3. Create Mock Implementations for Testing.235. 4. Inject Dependencies with Default Parameters.246. 5. Write Tests with Swift Testing.2526- Resolve its `scripts/`, `references/`, `assets/` and template paths against `references/upstream/`, not the project root. Source skill names map through [SOURCE.json](SOURCE.json) (`bossku skills show <id>`). ECC slash commands, agent names and package-root paths are not installed: use what this host has, or say what is missing.27- Check the project's tool versions and the source prerequisites first: read `swift-tools-version` in `Package.swift`, run `xcodebuild -version` and `swift --version`, and check whether the test targets import `Testing` or `XCTest`. Check the latest stable Swift release at swift.org/download at work time, and use only APIs present in the installed version. A documented command is an example, not permission to install packages, add an account, expose secrets, upload private content or change an external service.2829## Done when3031Apply the source “Core Pattern”, “1. Define Small, Focused Protocols” guidance to the concrete request. Return the resulting swift protocol-based dependency injection for testing artifact or diagnosis with source/command evidence for each conclusion.3233- Type traps: a mock must return the shapes production sends (a number as text, a missing key, `null`). Feed it recorded, sanitised production responses, not only happy-path values.34- Concurrency: a mock that records calls must be safe under parallel tests. Use an actor or a lock, or the tests can flake.35- Zero downtime: change a protocol in steps. Add the new requirement to the protocol, with a default implementation in a protocol extension. Move the callers and the mock conformers to the new requirement. Then remove the extension default once every conformer implements it, so each step builds and ships.36- Full quality bar: `../../references/checklists/engineering-quality-bar.md`.3738If an input, provider, runtime or authorisation is missing, stop that step, carry on with read-only analysis, and say exactly what is missing. A generated plan alone is not completion.3940Adapted from the locally reviewed `affaan-m/ECC` snapshot: provenance and hashes in [SOURCE.json](SOURCE.json), the original text and resources in [references/upstream/](references/upstream/SKILL.md) under its [MIT licence](references/upstream/LICENSE). Copyright (c) 2026 Affaan Mustafa.