Skip to content

skillTesting

Test Mocking Strategies

Picks the right test double for each dependency (real object, fake, stub or spy) so tests stay fast without mocking away the behavior they should verify.

npx workspacetemplates add test-mocking-strategies --tool claude-code

Writes 1 file to .claude/skills/test-mocking-strategies/

.claude/skills/test-mocking-strategies/SKILL.md27 lines · 1.6 KBFile
---
name: test-mocking-strategies
description: Picks the right test double for each dependency (real object, fake, stub or spy) so tests stay fast without mocking away the behavior they should verify.
---

# Test mocking strategies

Use the least fake double that keeps the test fast and deterministic.

## Decide per dependency

1. **Real object** when it is pure, in memory and cheap: value types, parsers, formatters. Never mock these.
2. **Fake** (a working simplified implementation) for stateful collaborators you own: an in-memory repository, a fake clock, a local queue. Share one fake across the suite and test it against the same contract as the real one.
3. **Stub** for a canned answer from something slow or external: an HTTP client returning a fixed response, including error and timeout cases.
4. **Spy or mock** only when the call itself is the behavior: an email sent, a payment charged, an event published. Assert the arguments that matter, not the call order.

## Rules

- Mock at the boundary you own, such as your own payment client wrapper, not the third-party SDK's internals.
- Assert outcomes (return value, stored state, emitted event) before interactions.
- Never mock the class under test, and never stub a private method.
- If a test needs more than three doubles, the unit has too many dependencies: say so and suggest the split.
- Keep stubs honest: build responses from recorded real payloads or the published schema.

## Report

For a review: each test with a smell, the double it uses, the better choice and why. For new tests: which doubles you chose and the reason for each.

What it does

Over-mocked tests pass while the product is broken, because they only prove the code calls what the test told it to call. This skill gives the agent a decision order for every collaborator, with the real thing first.

It also audits existing tests for the common traps: mocking the unit under test, asserting call counts instead of outcomes, and stubs that drift from the real interface.

Example

you › Review the mocks in src/billing/__tests__ and tell me which ones hide bugs.

agent › 3 of 11 tests mock InvoiceCalculator, the class they test, so they assert nothing about totals. Replace with the real calculator and a fake clock. chargeCustomer.test asserts call order on the Stripe SDK; switch to checking the PaymentResult and the saved status. Fake repository added in tests/fakes/ with a contract test.

Questions

Is it ever fine to mock a database?
Rarely. A fake in memory or a real throwaway database catches query and mapping mistakes that a mock cannot, so prefer those unless the test is truly about orchestration.
Does this depend on a test framework?
No. The decision order applies to Jest, pytest, JUnit, Go and others. It uses whatever mocking tools your project already has.
  • Contract Testing

    SkillTesting

    Free
  • E2E Test Design

    SkillTesting

    Free
  • Flaky Test Diagnosis

    SkillTesting

    Free