Swift tdd
Skill UnleashedServices/unleashed-mail-plugin/skills/swift-tdd
Test-driven development workflow for Swift/XCTest. Activates when implementing new features, writing tests, or refactoring existing code in UnleashedMail. Enforces red-green-refactor discipline with GRDB-aware test patterns.From its SKILL.md
npx -y skills add UnleashedServices/unleashed-mail-plugin --skill swift-tddAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
4.0 KB, as published. Nobody here has run it
Swift TDD — Red/Green/Refactor for UnleashedMail
When This Skill Applies
Use this workflow whenever you are:
- Implementing a new feature or user story
- Adding test coverage to existing code
- Refactoring code that already has (or should have) tests
- Fixing a bug (write a failing test that reproduces it first)
Workflow
Phase 1: RED — Write a Failing Test First
-
Identify the behavior you're implementing. State it as a single sentence:
"When [condition], [component] should [expected behavior]"
-
Create or locate the test file in
Unleashed MailTests/mirroring the source path underUnleashed Mail/Sources/(note the spaces in directory names).- Source:
Unleashed Mail/Sources/ViewModels/InboxViewModel.swift - Test:
Unleashed MailTests/ViewModels/InboxViewModelTests.swift
- Source:
-
Write the minimal failing test:
import XCTest @testable import UnleashedMail @MainActor final class InboxViewModelTests: XCTestCase { func test_fetchEmails_updatesMessageList() async throws { // Arrange let sut = InboxViewModel(emailService: MockEmailService()) // Act await sut.fetchEmails() // Assert XCTAssertFalse(sut.messages.isEmpty, "Messages should be populated after fetch") } } -
Run the test and confirm it fails:
set -o pipefail # surface xcodebuild's real exit through the `| tail` pipe xcodebuild test -scheme "Unleashed Mail" -destination 'platform=macOS' \ -only-testing:"Unleashed MailTests/InboxViewModelTests/test_fetchEmails_updatesMessageList" 2>&1 | tail -20- If it does NOT fail, your test is not testing new behavior. Rewrite it.
Phase 2: GREEN — Minimal Implementation
- Write the minimum code to make the failing test pass.
- Do NOT add extra logic, optimizations, or edge-case handling yet.
- Run the single test again and confirm it passes.
- Run the full test suite to ensure no regressions:
set -o pipefail # without it, `| tail` returns 0 and masks a failing xcodebuild xcodebuild test -scheme "Unleashed Mail" -destination 'platform=macOS' 2>&1 | tail -30
Phase 3: REFACTOR — Clean Up
- Look for duplication, unclear naming, or structural issues.
- Extract helpers, rename for clarity, simplify conditionals.
- Run the full test suite after every refactor step — tests must stay green.
GRDB Test Patterns
For database-related tests, use an in-memory database:
import GRDB
func makeTestDatabase() throws -> DatabaseQueue {
let dbQueue = try DatabaseQueue()
try dbQueue.write { db in
try db.create(table: "email") { t in
t.autoIncrementedPrimaryKey("id")
t.column("subject", .text).notNull()
t.column("sender", .text).notNull()
t.column("receivedAt", .datetime).notNull()
}
}
return dbQueue
}
Mock Patterns
Use protocol-based dependency injection for testability:
protocol EmailServiceProtocol: Sendable {
func fetchInbox() async throws -> [Email]
func send(_ draft: Draft) async throws
}
@MainActor
final class MockEmailService: EmailServiceProtocol, @unchecked Sendable {
var stubbedEmails: [Email] = []
var sendCallCount = 0
func fetchInbox() async throws -> [Email] { stubbedEmails }
func send(_ draft: Draft) async throws { sendCallCount += 1 }
}
Hard Rules
- NEVER skip the RED phase. If you cannot write a failing test first, you don't understand the requirement well enough.
- ONE behavior per test. If you need "and" in the test name, split it.
- No production code without a corresponding test.
- If a test is flaky, fix it immediately — do not mark it as skipped.