SKILL

swift-protocol-di-testing

テスト可能なSwiftコードのためのプロトコルベースの依存性注入——焦点を絞ったプロトコルとSwift Testingを使用してファイルシステム、ネットワーク、外部APIをモックする。

Original · translation is not supported for this source language

Target: Software development & testing. Automatically collected · unverified.

Excerpt from the author’s documentation

Extracted from a fixed version of SKILL.md in its original language. This is the author’s documentation; installation, compatibility and runtime results have not been verified by this site.

Read the original Japanese text

Topics in the source

  • プロトコルベースのSwift依存性注入テスト
  • 起動条件
  • コアパターン
  • 1. 小さく焦点を絞ったプロトコルを定義する
  • 2. デフォルト(本番用)実装を作成する
  • 3. テスト用のモック実装を作成する
  • 4. デフォルトパラメーターで依存関係を注入する
  • 5. Swift Testingを使用してテストを書く

外部の依存関係(ファイルシステム、ネットワーク、iCloud)を小さく焦点を絞ったプロトコルとして抽象化することで、SwiftコードをテストしやすくするパターンI/Oなしの決定論的テストをサポートする。 ファイルシステム、ネットワーク、または外部APIにアクセスするSwiftコードを書く場合 実際の障害を起こさずにエラー処理パスをテストする必要がある場合 異なる環境(アプリ、テスト、SwiftUIプレビュー)で動作するモジュールを構築する場合 Swift並行処理(Actor、Sendable)をサポートするテスト可能なアーキテクチャを設計する場合 各プロトコルは1つの外部関心事のみを処理する。 本番コードはデフォルト値を使用し、テストはモックを注入する。 単一責任:各プロトコルは1つの関心事を処理する——多くのメソッドを持つ「ゴッドプロトコル」を作らない Sendable 一貫性:プロトコルがActor境界をまたいで使用される場合に必要 デフォルトパラメーター:本番コードは実際の実装をデフォルトで使用する。テストだけがモックを指定する必要がある エラーのモック:障害パスをテストするために設定可能なエラープロパティを持つモックを設計する 境界のみをモック:外部の依存関係(ファイルシステム、ネットワーク、API)をモックし、内部型はモックしない すべての外部アクセスをカバーする単一の大きなプロトコルを作成する 外部の依存関係を持たない内部型をモックする 適切な依存性注入の代わりに #if DEBUG 条件文を使用する Actorと組み合わせて使用する際に Sendable 一貫性を忘れる 過度な設計:型が外部の依存関係を持たない場合、プロトコルは必要ない ファイルシステム、ネットワーク、または外部APIに触れるあらゆるSwiftコード 実際の環境では引き起こすことが難しいエラー処理パスをテストする場合 アプリ、テスト、SwiftUIプレビューのコンテキストで動作するモジュールを構築する場合 Swift並行処理(Actor、構造化並行処理)を採用したテスト可能なアーキテクチャが必要なアプリ

Source repository
affaan-m/ECC
Source file
docs/ja-JP/skills/swift-protocol-di-testing/SKILL.md
Fixed revision
d2ef2680594f9a17526a6e1a2e18b3d242c5d17b
Read the complete original documentation ↗

Sources and evidence

This record was collected automatically. Its documentation, suitability and runtime behavior have not been manually verified.

Author: affaan-m/ECC · original repository metadata. Documentation review: Not reviewed. Last source check: 2026-11-09T14:01:49.747Z.

Categories are inferred from names, descriptions and paths. They do not establish compatibility.

License and attribution

MIT. Third-party content and copyright remain with their authors. Refer to the original license and terms.

Original license ↗

Synchronization, copying and user-reported success do not mark a resource as runtime-tested.

Limitations and notes

  • Collecting metadata does not mean this site has installed, adapted, runtime-tested or security-reviewed the resource.