Testing without mocks

From hugoduncan/psi · View interactive version

Testing without mocks is an AI agent skill from the GitHub repository hugoduncan/psi. Apply James Shore's "Testing Without Mocks" pattern language to write tests that are narrow, sociable, state-based, and free of mocks/spies. Use this skill when writing or refactoring tests for any codebase, when the user asks to test code without mocks, when applying Nullable infrastructure pattern

How to use this skill

Skill file

Testing Without Mocks

Apply James Shore's pattern language for writing tests that are narrow, fast, deterministic, and mock-free.

Full reference: https://www.jamesshore.com/v2/projects/nullables/testing-without-mocks

Goals

Write tests that satisfy ALL of these properties:

  1. No broad tests required — only narrow tests focused on specific behaviors; smoke tests are a safety net, not the strategy
  2. Easy refactoring — test behavior/outcomes, not interactions or method calls between objects
  3. Readable tests — straightforward arrange/act/assert; tests document externally-visible behavior
  4. No magic — no DI frameworks, auto-mocking, or reflection tricks required
  5. Fast and deterministic — entire suite runs in 1–2 seconds with no flaky failures

Core Decision: Which Pattern Category?

Classify the code under test, then apply the right pattern group:

Pattern Summary

For detailed pattern descriptions and examples, see references/patterns.md.

Foundational Patterns

Architectural Patterns

Logic Patterns

Infrastructure Patterns

Nullability Patterns

Legacy Code Patterns

Workflow: Writing a New Test

  1. Classify the code: pure logic, infrastructure wrapper, or orchestration?
  2. If pure logic → write state-based tests directly with real collaborators
  3. If infrastructure wrapper → write narrow integration tests against the real system, then add createNull() with Embedded Stub
  4. If orchestration → ensure dependencies are Nullable, then write tests using createNull() with Configurable Responses and Output Tracking
  5. Verify: does the test assert on outputs/state only (not on calls to dependencies)?
  6. Verify: does the test use arrange/act/assert structure?
  7. Verify: would a structural refactoring break this test? If yes, revise.

Workflow: Removing Mocks from Existing Tests

  1. Identify the mock/spy being used
  2. Determine what the mock's target is: infrastructure or logic?
  3. If logic → remove the mock; use real collaborator (sociable test)
  4. If infrastructure → make the infrastructure class Nullable, use createNull() with Configurable Responses and Output Tracking
  5. Delete the mock setup; replace with createNull()-based instantiation
  6. Assert on outputs/state instead of verifying mock interactions
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.