Skip to content
generating-test-doubles logo

Test Doubles Generator

generating-test-doubles

Generate mocks, stubs, spies, and fakes for dependency isolation.

SKILL.md

Full skill instructions

Test Doubles Generator

Overview

Generate mocks, stubs, spies, and fakes to isolate units under test from external dependencies. Supports Jest mocks, Sinon.js stubs, Python unittest.mock, Go interfaces, and testdouble.js patterns.

Prerequisites

  • Testing framework installed (Jest, Vitest, Mocha, pytest, JUnit 5, or Go testing)
  • Mocking library available (Sinon.js, testdouble.js, unittest.mock, Mockito, or gomock)
  • TypeScript strict mode enabled for type-safe mocks (if applicable)
  • Source code with clear interface or class boundaries for dependency injection

Instructions

  1. Scan the codebase with Glob and Grep to identify modules with external dependencies (database clients, HTTP clients, file system access, third-party SDKs).
  2. Read each module under test and catalog its dependency interfaces -- list every method signature, return type, and side effect.
  3. Determine the appropriate test double type for each dependency:
    • Stub: Returns canned data, no behavior verification (use for database queries returning fixed datasets).
    • Mock: Verifies interactions -- call count, argument matching, call order (use for email senders, event emitters).
    • Spy: Wraps real implementation while recording calls (use when partial behavior is needed).
    • Fake: Lightweight working implementation (use for in-memory repositories replacing real databases).
  4. Generate test double files following the project's existing test directory structure (e.g., __mocks__/, test/​doubles/, testutil/).
  5. For each test double, implement:
    • Factory function or class matching the dependency interface exactly.
    • Configurable return values via builder pattern or method chaining.
    • Call recording for assertion (arguments, call count, call order).
    • Reset/​restore mechanism for cleanup between tests.
  6. Wire test doubles into existing test files using the framework's dependency injection pattern (jest.mock(), @patch, constructor injection, or Go interface substitution).
  7. Validate all test doubles compile and pass type checks by running tsc --noEmit or equivalent.

Output

  • Test double source files (one per dependency) placed in the project's mock directory
  • Factory functions with TypeScript generics or equivalent type safety
  • Jest __mocks__ auto-mock modules where applicable
  • Updated test files wired to use generated doubles instead of real dependencies
  • Summary listing each double, its type (mock/​stub/​spy/​fake), and the interface it replaces

Error Handling

ErrorCauseSolution
TypeError: X is not a functionMock missing a method from the real interfaceRegenerate the double from the current interface definition; add the missing method
Mock leaking between testsShared mock state not reset in afterEachAdd jest.restoreAllMocks() or sinon.restore() in teardown hooks
Type mismatch on mock return valueReturn type does not match interface contractUse as ReturnType<typeof fn> or update the mock factory to return the correct type
Spy not recording callsSpy created after the function was already boundCreate spies before the module under test imports the dependency
Over-mocking hides real bugsToo many layers replaced with fakesLimit mocks to true external boundaries (I/​O, network); let pure logic run unmocked

Examples

Jest mock factory for a UserRepository:

// __mocks__/​userRepository.ts
export const createMockUserRepo = () => ({
  findById: jest.fn().mockResolvedValue({ id: '1', name: 'Test User' }),
  save: jest.fn().mockResolvedValue(undefined),
  delete: jest.fn().mockResolvedValue(true),
});

Python unittest.mock patch for an HTTP client:

from unittest.mock import patch, MagicMock

@patch('myapp.client.requests.get')
def test_fetch_data(mock_get):
    mock_get.return_value = MagicMock(status_code=200, json=lambda: {"key": "value"})  # HTTP 200 OK
    result = fetch_data("https://api.example.com/​data")
    assert result == {"key": "value"}
    mock_get.assert_called_once()

Go interface-based fake:

type FakeStore struct {
    data map[string]string
}
func (f *FakeStore) Get(key string) (string, error) {
    v, ok := f.data[key]
    if !ok { return "", ErrNotFound }
    return v, nil
}

Resources

More skills from jeremylongshore

openrouter-compliance-review logo
jeremylongshore/claude-code-plugins-plus-skills

openrouter-compliance-review

Review OpenRouter integration for regulatory compliance (SOC2, GDPR,

2.8K 0
View
maintainx-data-handling logo
jeremylongshore/claude-code-plugins-plus-skills

maintainx-data-handling

Data synchronization, ETL patterns, and data management for MaintainX.

2.8K 0
View
groq-core-workflow-a logo
jeremylongshore/claude-code-plugins-plus-skills

groq-core-workflow-a

Execute Groq primary workflow: chat completions with tool use and JSON

2.8K 0
View
validating-csrf-protection logo
jeremylongshore/claude-code-plugins-plus-skills

validating-csrf-protection

Validate CSRF protection implementations for security gaps. Use when

2.8K 0
View
api-contract logo
jeremylongshore/claude-code-plugins-plus-skills

api-contract

Configure this skill should be used when the user asks about "API contract",

2.8K 0
View
ideogram-incident-runbook logo
jeremylongshore/claude-code-plugins-plus-skills

ideogram-incident-runbook

Execute Ideogram incident response with triage, mitigation, and postmortem.

2.8K 0
View
gamma-sdk-patterns logo
jeremylongshore/claude-code-plugins-plus-skills

gamma-sdk-patterns

Reusable patterns for the Gamma REST API (no SDK exists).

2.8K 0
View
deepgram-sdk-patterns logo
jeremylongshore/claude-code-plugins-plus-skills

deepgram-sdk-patterns

Apply production-ready Deepgram SDK patterns for TypeScript and Python.

2.8K 0
View
openrouter-upgrade-migration logo
jeremylongshore/claude-code-plugins-plus-skills

openrouter-upgrade-migration

Migrate to OpenRouter from direct provider APIs or upgrade between SDK/model

2.8K 0
View
openrouter-common-errors logo
jeremylongshore/claude-code-plugins-plus-skills

openrouter-common-errors

Diagnose and fix common OpenRouter API errors. Use when encountering

2.8K 0
View
linear-cost-tuning logo
jeremylongshore/claude-code-plugins-plus-skills

linear-cost-tuning

Optimize Linear API usage, reduce unnecessary calls, and maximize

2.8K 0
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View