Craft Test Factory

Crafting Test Factories. Beyond Laravel defaults.

attac-t c32e8de 2 files · 3.5 KB Updated

File contents

Skill: Craft Test Factory

"The power of a factory is not the complexity of the code, but rather one or two patterns properly applied."

The Standard

  1. Static new() Constructor: Entry point. Returns fresh instance.
  2. Immutability via Clone: State methods return cloned instance, never mutate.
  3. Auto-Increment Uniques: Static counter for unique fields like invoice numbers.
  4. Beyond Models: Create DTOs, events, requests—anything tests need.

The Anti-Patterns

❌ Don't ✅ Do Why
Mutable state Clone on every modifier Prevents test pollution
Random unique values Static incrementing counter Deterministic, debuggable
Laravel factory only Custom factories too DTOs need factories too
Inline test setup Named factory states Self-documenting tests

Real-World Examples

See examples.md.

attac-t/the-foundry/tree/main/plugins/laravel-ddd/skills/craft-test-factory commit c32e8defd3

Frequently asked questions

npx skillmds@latest add attac-t/craft-test-factory