Coding
AI Prompt for Mocking Dependencies
Create clean test mocks with this AI prompt: fakes, stubs and spies for your dependencies, with verification that your code used them correctly.
The prompt
Role: You are a testing expert who creates mocks that are faithful, minimal, and maintainable. Context: Language: [LANGUAGE], framework: [FRAMEWORK]. Code under test: [CODE_UNDER_TEST]. Dependencies to mock: [DEPENDENCIES with what they do]. Mock style: [STYLE: manual mocks / framework mocks / fake implementations]. Task: 1) For each dependency, create the mock/fake: only the methods the code under test actually uses, with realistic return values. 2) Show how to inject it (constructor, parameter, DI container — match the codebase). 3) Write the test demonstrating the mock in use, INCLUDING verification that the code called the dependency correctly (arguments, call count). 4) Warn about over-mocking: what should stay real in this test. Format: Mocks first, then injection, then the test with verifications, then the over-mocking warning. Constraints: Mock at the boundary (the dependency's public interface), not internals. Mocks must reflect the real dependency's contract — note any assumption you made about it. Do not mock the thing you are testing.
Variables to fill in
Replace each [variable] in the prompt with your own details:
[LANGUAGE] [FRAMEWORK] [CODE_UNDER_TEST] [DEPENDENCIES] [STYLE]
Example input
LANGUAGE=TypeScript, DEPENDENCY=Stripe API client + Postgres via Prisma, TEST=checkout function, FRAMEWORK=Vitest, MOCK_STYLE=manual mocks
How to use this prompt
List exactly what the dependency does in your test (returns what, throws when) — mocks must mimic the contract. State your mock style; framework idioms differ.
Tips for the best results
- Verify mock interactions (called with what, how often) — that is where mock tests earn their keep.
- Ask it to list the contract assumptions so to check them against the real dependency.
- Prefer fakes over mocks for stable interfaces; they survive refactoring better.
Frequently asked questions
Do mocks make tests meaningless?
It writes the mocks AND shows the verification (assert the mock was called correctly) — mocks without verification prove nothing.
When should I use a fake instead of a mock?
For stable interfaces you exercise heavily — fakes survive refactoring while mocks encode call patterns. The prompt helps you choose per dependency.