swift-protocol-di-testing

Featured

Protocol-based dependency injection for testable Swift code — mock file system, network, and external APIs using focused protocols and Swift Testing.

Testing & QA 308,704 stars 45417 forks Updated 2 days ago MIT

Install

View on GitHub

Quality Score: 96/100

Stars 20%
100
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# Swift Protocol-Based Dependency Injection for Testing Patterns for making Swift code testable by abstracting external dependencies (file system, network, iCloud) behind small, focused protocols. Enables deterministic tests without I/O. ## When to Activate - Writing Swift code that accesses file system, network, or external APIs - Need to test error handling paths without triggering real failures - Building modules that work across environments (app, test, SwiftUI preview) - Designing testable architecture with Swift concurrency (actors, Sendable) ## Core Pattern ### 1. Define Small, Focused Protocols Each protocol handles exactly one external concern. ```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. Create Default (Production) Implementations ```swift public struct DefaultFileSystemProvider: FileSystemProviding { public init() {} public func containerURL(for purpose: Purpose) -> URL? { FileManager.default.url(forUbiquityContaine...

Details

Author
affaan-m
Repository
affaan-m/everything-claude-code
Created
10 months ago
Last Updated
2 days ago
Language
JavaScript
License
MIT

Integrates with

Similar Skills

Semantically similar based on skill content — not just same category

Web & Frontend Listed

swift-dependency-injection

Design injectable seams so Swift services can be swapped for fakes in tests. Use when a type reaches for `URLSession.shared`, `Date()`, `UUID()`, `random(in:)` or a singleton; when asked "how do I inject CloudKit / network / clock" or "should this be a singleton"; when choosing constructor vs `@Environment` / `EnvironmentKey` vs `@TaskLocal` injection; when writing the `makeApp()` composition root; when evaluating swift-dependencies or Factory. Does NOT choose the test framework or snapshot tooling (swift-testing-baseline) or the target layout that hosts the root (swiftpm-modularization).

18 Updated 1 weeks ago
wei18
Testing & QA Listed

swift-test-strategy

iOS test strategy across Swift Testing (@Test / #expect / #require / parameterized @Test(arguments:) / traits / suites) and XCTest (UI tests, performance metrics), XCUITest automation (XCUIApplication, launch-argument test seams), and swift-snapshot-testing. Defines a 3-layer taxonomy (unit / integration / UI) mapped to iOS test targets, async patterns for actor-isolated services, and RED-first guidance. Use when writing or reviewing iOS tests, choosing Swift Testing vs XCTest, structuring test targets, testing async/actor code, or planning coverage for a feature/bugfix. Not for non-test app code (swift / swiftui / ios-platform); output is RED-first test files plus a 3-layer coverage plan. Requires the swift module.

2 Updated 2 days ago
hmj1026
Testing & QA Featured

swift-testing

Swift testing: XCTest, Swift Testing framework, async patterns.

451 Updated 2 days ago
notque