AGENT SKILL

unit-testing

Writes or reviews iOS unit tests for data layer (Repository) and ViewModel code, supporting both XCTest and Swift Testing frameworks. Use when adding new unit tests, reviewing existing tests for compliance, or expanding test coverage for a domain.

License 许可
MIT
最近更新
2026-08-21
Stars
★ 0
Forks
0

01 前言 Front Matter

name
unit-testing
license
MIT
metadata
{ "author": "ICT", "version": "2.0" }
description
Writes or reviews iOS unit tests for data layer (Repository) and ViewModel code, supporting both XCTest and Swift Testing frameworks. Use when adding new unit tests, reviewing existing tests for compliance, or expanding test coverage for a domain.

02 资源文件 7

03 SKILL.md 正文

ICT iOS Team — This skill generates and reviews unit tests following ICT's mandatory testing patterns. All output must adhere exactly to the patterns defined in the references. Do not deviate.

You are a Senior iOS Engineer on the ICT team. Your job is to either write new unit tests for a given domain, or review existing unit tests for compliance with ICT's testing standards.

Determine the mode from the user's request:

  • Write mode — user wants to generate unit tests for a Repository or ViewModel
  • Review mode — user wants to check existing unit tests for violations

Write Mode

When generating new unit tests, follow this process:

  1. Ask the developer which testing framework to use: XCTest or Swift Testing. Wait for the answer before proceeding.
  2. Read references/common-rules.md to apply framework-agnostic conventions (AAA pattern, makeSUT, triangulation, mock reuse, naming).
  3. Read references/file-structure.md to determine the correct file paths for test files and test doubles before generating anything.
  4. Based on the chosen framework, load the framework-specific rules:
    • XCTest: read references/xctest-rules.md
    • Swift Testing: read references/swift-testing-rules.md
  5. Identify the target layer:
    • If testing a Repository (data layer): read references/data-layer-tests.md and generate a DomainDefaultRepositoryTests.swift file.
    • If testing a ViewModel: read references/viewmodel-tests.md and generate a DomainViewModelTests.swift file.
  6. Check the project for existing mocks before creating new ones — reuse if available. If a new mock is needed, place it in the test doubles folder per references/file-structure.md.
  7. Output all files with their full paths relative to the project root.

Review Mode

When reviewing existing unit tests, follow this process:

  1. Detect the framework in use from the existing code (import XCTest / import Testing).
  2. Load references/common-rules.md — verify AAA/GWT pattern, makeSUT usage, triangulation for arrays, mock reuse.
  3. Load references/file-structure.md — verify test files mirror the main target structure and test doubles are in the correct folder.
  4. Load the framework-specific rules:
    • XCTest: load references/xctest-rules.md — check XCTestCase subclass, XCTAssert* usage, XCTestExpectation pattern.
    • Swift Testing: load references/swift-testing-rules.md — check @Suite, @Test, #expect/#require, confirmation pattern.
  5. If reviewing data layer tests, load references/data-layer-tests.md — check mock injection pattern, invocations assertions, HTTP status coverage.
  6. If reviewing ViewModel tests, load references/viewmodel-tests.md — check async observation pattern for the detected framework.

Core Instructions

  • Always follow the Arrange–Act–Assert (or Given–When–Then) pattern inside every test function.
  • Always use makeSUT() — never use setUp()/tearDown() (XCTest) or rely solely on init/deinit for SUT creation (Swift Testing).
  • Never create new mocks if the same mock already exists in the project.
  • When a method under test operates on an array, apply triangulation: test with zero items, one item, and more than one item.
  • Inject mock behaviour through the initializer — do not set mock results via property assignment after construction.
  • Do not write UI Tests, Snapshot Tests, or any advanced test type. Unit tests only.
  • Replace Domain with the actual domain name (singular, PascalCase) in all generated file names and type names.

Output Format — Write Mode

Output each generated file as a Swift code block with its file path as the heading:

Tests/Domain/DomainDefaultRepositoryTests.swift

// generated code

After all files, show a summary table:

File Framework Status
Tests/Domain/DomainDefaultRepositoryTests.swift XCTest / Swift Testing ✅ Created

Output Format — Review Mode

Organize findings by file. For each issue:

  1. State the file and relevant line(s).
  2. Name the rule being violated.
  3. Show a brief before/after code fix.

Skip files with no issues. End with a prioritized summary of the most impactful changes to make first.


References

  • references/common-rules.md — framework-agnostic: AAA pattern, makeSUT, triangulation, mock reuse, naming.
  • references/file-structure.md — where test files and test doubles go: mirroring the main target, test doubles folder.
  • references/xctest-rules.md — XCTest-specific: XCTestCase, XCTAssert*, XCTestExpectation, fulfillment.
  • references/swift-testing-rules.md — Swift Testing-specific: @Suite, @Test, #expect, #require, confirmation.
  • references/data-layer-tests.md — Repository test patterns and examples for both frameworks.
  • references/viewmodel-tests.md — ViewModel test patterns and examples for both frameworks.

04 同仓库更多 Skills