swiftdata-testing

Writes and reviews SwiftData test code using in-memory containers, mock repositories, and actor-safe patterns.

1|Updated Aug 9, 2026
One-click install
npx skills add https://github.com/OMIXEC/all-in-one-swift-skills --skill swiftdata-testing-omixec
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: swiftdata-testing
Source: https://github.com/OMIXEC/all-in-one-swift-skills/tree/main/skills/swiftdata-testing
Command: npx skills add https://github.com/OMIXEC/all-in-one-swift-skills --skill swiftdata-testing-omixec

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? SwiftData tests often fail intermittently or silently corrupt device state because they share ModelContexts, write to the real persistent store, or cross @ModelActor boundaries incorrectly. This Skill writes and reviews SwiftData test code so it runs in isolation, uses in-memory storage, and catches failure modes specific to @Model, @ModelActor, and Decimal money values. ## Core Features & Use Cases - In-Memory Test Fixtures: Builds fresh ModelContainer fixtures with isStoredInMemoryOnly per test, eliminating cross-test state leakage and device-store mutation. - Mock Repository Pattern: Replaces real ModelContainers in ViewModel unit tests with protocol-based in-memory mocks that run in microseconds. - Actor-Safe Testing: Enforces DTO/PersistentIdentifier patterns across @ModelActor boundaries instead of passing @Model instances. - Specialized Assertions: Covers Decimal money-value exact-equality assertions and content-hash dedup idempotency tests for CSV/API imports. - Use Case: When reviewing a SwiftData-backed app's test suite, the Skill flags a shared ModelContext across tests, a migrationPlan applied to an in-memory config, and epsilon-based Decimal comparisons, then shows before/after fixes. ## Quick Start Ask the assistant to review the SwiftData test files in this project and fix any tests that share containers, touch the real store, or cross actor boundaries.

Frequently Asked Questions about swiftdata-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write unit tests for SwiftData repositories?▼

Create a fresh ModelContainer per test with a ModelConfiguration using isStoredInMemoryOnly: true, listing every @Model type in the Schema array. Never share a ModelContext across tests, since shared state causes order-dependent failures.

How do I test a ViewModel that uses SwiftData?▼

Do not construct a real ModelContainer for ViewModel tests. Define a repository protocol, inject a hand-written in-memory mock with recording fields like saveCalled, and seed its backing array directly in the test's arrange step.

Why does SwiftData save fail with an in-memory container and migration plan?▼

Applying a SchemaMigrationPlan to an isStoredInMemoryOnly configuration causes save() to fail intermittently, because an in-memory store starts empty and has nothing to migrate. Branch on isStoredInMemoryOnly and omit the migration plan for test containers.

Can I pass a SwiftData @Model instance into a @ModelActor in tests?▼

No. @Model instances are not Sendable and must never cross an actor boundary, even in test code. Pass a PersistentIdentifier or a value-type DTO and re-fetch the model inside the target actor.

Should I compare Decimal money values with a tolerance in tests?▼

No. Decimal supports exact equality, so compare with == and never an epsilon tolerance. If a test needs a tolerance to pass, a Double leaked into the calculation upstream and that is the bug to fix.

Why is my SwiftData dedup import test flaky?▼

Flaky dedup tests usually use Date.now instead of a fixed Date, so two calls milliseconds apart produce different content hashes. Use a fixed Date constant and assert on the import summary's imported and skippedAsDuplicate counts, not just the final row count.