KS
Killer-Skills

swift-protocol-di-testing — how to use swift protocol di testing how to use swift protocol di testing, what is swift protocol dependency injection, swift protocol di testing vs manual mocks, swift protocol di testing alternative, swift protocol di testing install guide, swift protocol di testing setup tutorial, mock file system swift testing, protocol based dependency injection swift, testable swift code external APIs, deterministic swift tests without I/O

Verified
v1.0.0
GitHub

About this Skill

Perfect for Swift Development Agents needing advanced protocol-based dependency injection for testable code. swift-protocol-di-testing is a protocol-based dependency injection approach for creating testable Swift code. It abstracts external dependencies like file systems, network calls, and external APIs behind focused protocols using Swift Testing, enabling deterministic tests without actual I/O operations.

Features

Abstracts file system access behind focused protocols for testability
Mocks network calls and external APIs using protocol injection
Enables deterministic testing without triggering real I/O operations
Supports testing error handling paths without real failures
Builds modules that work across app, test, and SwiftUI preview environments
Uses Swift Testing framework for protocol-based mock implementations

# Core Topics

affaan-m affaan-m
[62.0k]
[7678]
Updated: 3/6/2026

Quality Score

Top 5%
86
Excellent
Based on code quality & docs
Installation
SYS Universal Install (Auto-Detect)
Cursor IDE Windsurf IDE VS Code IDE
> npx killer-skills add affaan-m/everything-claude-code/swift-protocol-di-testing

Agent Capability Analysis

The swift-protocol-di-testing MCP Server by affaan-m is an open-source Categories.official integration for Claude and other AI agents, enabling seamless task automation and capability expansion. Optimized for how to use swift protocol di testing, what is swift protocol dependency injection, swift protocol di testing vs manual mocks.

Ideal Agent Persona

Perfect for Swift Development Agents needing advanced protocol-based dependency injection for testable code.

Core Value

Empowers agents to abstract external dependencies like file systems, networks, and iCloud behind focused protocols using Swift Testing, enabling deterministic tests without I/O and promoting test-driven development with mock APIs.

Capabilities Granted for swift-protocol-di-testing MCP Server

Mocking file system interactions for unit testing
Isolating network dependencies for error handling tests
Building modular Swift code compatible with various environments

! Prerequisites & Limits

  • Requires Swift programming language
  • Focused on protocol-based dependency injection for testing purposes
Project
SKILL.md
5.7 KB
.cursorrules
1.2 KB
package.json
240 B
Ready
UTF-8
SKILL.md
Readonly

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
1// File system access 2public protocol FileSystemProviding: Sendable { 3 func containerURL(for purpose: Purpose) -> URL? 4} 5 6// File read/write operations 7public protocol FileAccessorProviding: Sendable { 8 func read(from url: URL) throws -> Data 9 func write(_ data: Data, to url: URL) throws 10 func fileExists(at url: URL) -> Bool 11} 12 13// Bookmark storage (e.g., for sandboxed apps) 14public protocol BookmarkStorageProviding: Sendable { 15 func saveBookmark(_ data: Data, for key: String) throws 16 func loadBookmark(for key: String) throws -> Data? 17}

2. Create Default (Production) Implementations

swift
1public struct DefaultFileSystemProvider: FileSystemProviding { 2 public init() {} 3 4 public func containerURL(for purpose: Purpose) -> URL? { 5 FileManager.default.url(forUbiquityContainerIdentifier: nil) 6 } 7} 8 9public struct DefaultFileAccessor: FileAccessorProviding { 10 public init() {} 11 12 public func read(from url: URL) throws -> Data { 13 try Data(contentsOf: url) 14 } 15 16 public func write(_ data: Data, to url: URL) throws { 17 try data.write(to: url, options: .atomic) 18 } 19 20 public func fileExists(at url: URL) -> Bool { 21 FileManager.default.fileExists(atPath: url.path) 22 } 23}

3. Create Mock Implementations for Testing

swift
1public final class MockFileAccessor: FileAccessorProviding, @unchecked Sendable { 2 public var files: [URL: Data] = [:] 3 public var readError: Error? 4 public var writeError: Error? 5 6 public init() {} 7 8 public func read(from url: URL) throws -> Data { 9 if let error = readError { throw error } 10 guard let data = files[url] else { 11 throw CocoaError(.fileReadNoSuchFile) 12 } 13 return data 14 } 15 16 public func write(_ data: Data, to url: URL) throws { 17 if let error = writeError { throw error } 18 files[url] = data 19 } 20 21 public func fileExists(at url: URL) -> Bool { 22 files[url] != nil 23 } 24}

4. Inject Dependencies with Default Parameters

Production code uses defaults; tests inject mocks.

swift
1public actor SyncManager { 2 private let fileSystem: FileSystemProviding 3 private let fileAccessor: FileAccessorProviding 4 5 public init( 6 fileSystem: FileSystemProviding = DefaultFileSystemProvider(), 7 fileAccessor: FileAccessorProviding = DefaultFileAccessor() 8 ) { 9 self.fileSystem = fileSystem 10 self.fileAccessor = fileAccessor 11 } 12 13 public func sync() async throws { 14 guard let containerURL = fileSystem.containerURL(for: .sync) else { 15 throw SyncError.containerNotAvailable 16 } 17 let data = try fileAccessor.read( 18 from: containerURL.appendingPathComponent("data.json") 19 ) 20 // Process data... 21 } 22}

5. Write Tests with Swift Testing

swift
1import Testing 2 3@Test("Sync manager handles missing container") 4func testMissingContainer() async { 5 let mockFileSystem = MockFileSystemProvider(containerURL: nil) 6 let manager = SyncManager(fileSystem: mockFileSystem) 7 8 await #expect(throws: SyncError.containerNotAvailable) { 9 try await manager.sync() 10 } 11} 12 13@Test("Sync manager reads data correctly") 14func testReadData() async throws { 15 let mockFileAccessor = MockFileAccessor() 16 mockFileAccessor.files[testURL] = testData 17 18 let manager = SyncManager(fileAccessor: mockFileAccessor) 19 let result = try await manager.loadData() 20 21 #expect(result == expectedData) 22} 23 24@Test("Sync manager handles read errors gracefully") 25func testReadError() async { 26 let mockFileAccessor = MockFileAccessor() 27 mockFileAccessor.readError = CocoaError(.fileReadCorruptFile) 28 29 let manager = SyncManager(fileAccessor: mockFileAccessor) 30 31 await #expect(throws: SyncError.self) { 32 try await manager.sync() 33 } 34}

Best Practices

  • Single Responsibility: Each protocol should handle one concern — don't create "god protocols" with many methods
  • Sendable conformance: Required when protocols are used across actor boundaries
  • Default parameters: Let production code use real implementations by default; only tests need to specify mocks
  • Error simulation: Design mocks with configurable error properties for testing failure paths
  • Only mock boundaries: Mock external dependencies (file system, network, APIs), not internal types

Anti-Patterns to Avoid

  • Creating a single large protocol that covers all external access
  • Mocking internal types that have no external dependencies
  • Using #if DEBUG conditionals instead of proper dependency injection
  • Forgetting Sendable conformance when used with actors
  • Over-engineering: if a type has no external dependencies, it doesn't need a protocol

When to Use

  • Any Swift code that touches file system, network, or external APIs
  • Testing error handling paths that are hard to trigger in real environments
  • Building modules that need to work in app, test, and SwiftUI preview contexts
  • Apps using Swift concurrency (actors, structured concurrency) that need testable architecture

Related Skills

Looking for an alternative to swift-protocol-di-testing or building a Categories.official AI Agent? Explore these related open-source MCP Servers.

View All

flags

Logo of facebook
facebook

flags is a feature flag management system that enables developers to check flag states, compare channels, and debug feature behavior differences across release channels.

243.6k
0
Design

extract-errors

Logo of facebook
facebook

extract-errors is a skill that assists in extracting and managing error codes in React applications using yarn extract-errors command.

243.6k
0
Design

fix

Logo of facebook
facebook

fix is a technical skill that resolves lint errors, formatting issues, and ensures code quality in declarative, frontend, and UI projects

243.6k
0
Design

flow

Logo of facebook
facebook

Flow is a type checking system for JavaScript, used to validate React code and ensure consistency across applications

243.6k
0
Design