Generate Mock
Generate a mock or stub for an interface, service, or module so tests can isolate the unit under test. Triggers on 'mock this', 'stub the service', 'generate a mock for'. Produces a pytest `unittest.m
SKILL.md.claude/skills/skill-generate-mock/SKILL.md
GitHubPurpose
Provide deterministic, conventional mocks that match the test framework in use, with realistic synthetic return values and no accidental network calls.
When to Invoke
- A unit test needs to isolate a service dependency.
- A frontend component test needs to mock a fetch client.
- An integration test needs an MSW handler for an outbound call.
When NOT to Invoke
- For contract tests (use
/skill-api-contract-test). - For real integration tests where the dependency should actually run.
- For production code stubs.
Inputs
- target: symbol or module path to mock.
- framework:
pytest,vitest, ormsw. - scenario (optional):
success(default),error, orempty.
Procedure
- Resolve the target and read its signature(s).
- For
pytest: generate a fixture underapi/tests/fixtures/that patches the symbol withunittest.mock.MagicMockand sets a return value matching the Pydantic model. - For
vitest: generate avi.mock(...)block with a typed return value drawn from the existing builder or a literal. - For
msw: generate a handler underfrontend/src/test-utils/msw/returning a synthetic response. - Apply the
scenarioto choose return shape:success→ fully populated valid response.error→ raises / returns 4xx/5xx with a synthetic error body.empty→ empty list or null result.
- Write the mock to the conventional location.
Outputs
- New mock file at the conventional location, imported from the nearest test-utils barrel if one exists.
- Summary of the mocked symbol, framework, scenario, and return shape.
Safety
- Never invokes the real dependency during generation.
- Never embeds real credentials or tokens in mock payloads.
- Never mocks security-sensitive modules (auth, audit logging) without a reminder that the mock must not be reused in integration tests.
- FedRAMP: all mock payloads use synthetic IDs and
@example.testemails.
Examples
Example 1 — Pytest. Mock OpportunitySearchClient.query with a 3-item success payload.
Example 2 — Vitest. Mock fetchOpportunities to return an empty list, exercising the empty-state component.
Example 3 — MSW. Add a handler for GET /v1/opportunities/search returning a 500 to test error UI.
Related
.cursor/skills/skill-generate-factory/— factories for shared return values..cursor/skills/skill-api-contract-test/— contract tests, not mocks.