agentleFS
apexbymanishapexbymanish/claude-ai-agents-ios/.claude/skills/ios-testing-strategy/SKILL.md

ios-testing-strategy

Skill0 starsChanged 3 months agoMITView on GitHub

Step-by-step procedure for adding tests to existing Swift/iOS code, whether untested legacy code or new TDD work. Use when asked to "add test coverage", "write tests for this", "make this testable", or when starting TDD on new Swift code.

1 other project uses a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

8 rules. About 0 are common, 4 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Before writing a single test, find (or create) the seams that let you substitute real dependencies with test doubles:

    line 12
  2. Distinctive:

    If the code under test reaches a singleton directly, introduce a protocol and inject it — don't test through the singleton.

    line 17
  3. Very specific:

    If the class under test currently hardcodes LiveUserRepository() internally, that's the first fix: add the initializer parameter above …

    line 33
  4. Very specific:

    Use Swift Testing (@Test, #expect) for new code in projects on Swift 5.10+ / Xcode 16+.

    line 67
  5. Distinctive:

    Before implementing the fix/feature, run the test and confirm it fails for the *expected* reason (not a compile error, not a setup bug):

    line 74
  6. Very specific:

    Don't test that a SwiftUI View's body produces particular view types — that's an implementation detail and breaks on every refactor.

    line 94
  7. Very specific:

    Don't test third-party framework behavior (e.g.

    line 96
  8. Distinctive:

    Don't write a test for a private method by making it internal/public just to reach it — test through the public behavior that uses it.

    line 99

≈ “Common” is an estimate: at least 1 in 5 of the 372,429 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

iOS Testing Strategy

A concrete procedure, not just principles. Follow these steps in order.

1. Identify the seams

Before writing a single test, find (or create) the seams that let you substitute real dependencies with test doubles:

  • Networking, persistence (Core Data/SwiftData contexts), date/time, and any singleton (.shared) are the usual suspects.
  • If the code under test reaches a singleton directly, introduce a protocol and inject it — don't test through the singleton.
protocol UserRepository {
    func fetchUser(id: String) async throws -> User
}

final class LiveUserRepository: UserRepository { /* real network call */ }

final class ProfileViewModel {
    private let repository: UserRepository
    init(repository: UserRepository) { self.repository = repository }
}

If the class under test currently hardcodes LiveUserRepository() internally, that's the first fix: add the initializer parameter above (default it to the live implementation so call sites don't all need to change at once).

2. Write a test double, not a framework

A hand-written struct/class conforming to the protocol is almost always enough. Reach for a mocking library only if the project already has one.

final class StubUserRepository: UserRepository {
    var result: Result<User, Error> = .success(.mock)
    func fetchUser(id: String) async throws -> User { try result.get() }
}

3. Write the test against behavior, not implementation

Assert on outputs and observable state changes, not on which private methods got called.

@Test func profileLoadShowsFetchedUserName() async throws {
    let repository = StubUserRepository()
    repository.result = .success(User(id: "1", name: "Ada"))
    let viewModel = ProfileViewModel(repository: repository)

    await viewModel.load(id: "1")

    #expect(viewModel.displayName == "Ada")
}

Use Swift Testing (@Test, #expect) for new code in projects on Swift 5.10+ / Xcode 16+. Use XCTest for projects that haven't adopted Swift Testing yet — check the existing test target before picking one; don't mix frameworks in the same target without a reason.

4. Verify red before green

Before implementing the fix/feature, run the test and confirm it fails for the expected reason (not a compile error, not a setup bug):

Run: xcodebuild test -scheme <Scheme> -destination 'platform=iOS Simulator,name=iPhone 16' -only-testing:<Target>/<TestClass>/<testName>

Then implement the minimal code to pass, and re-run the same command to confirm green.

5. Prioritize by risk, not by percentage

Coverage percentage is a lagging indicator. Prioritize, in order:

  1. Business logic with branching (pricing, eligibility, state machines).
  2. Anything that has broken in production before.
  3. Public API of a module other code depends on.
  4. Pure UI layout code is usually not worth unit testing — that's what ios-ui-test-engineer and snapshot tests are for.

6. What NOT to test

  • Don't test that a SwiftUI View's body produces particular view types — that's an implementation detail and breaks on every refactor.
  • Don't test third-party framework behavior (e.g. that URLSession actually makes a network call) — test that your code calls it correctly, via the seam from step 1.
  • Don't write a test for a private method by making it internal/public just to reach it — test through the public behavior that uses it.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.