mcpField
M
mocking-stubbing

mocking-stubbing

github.com/aj-geddesTesting & quality373429 installsUpdated 34w ago
Repository

About mocking-stubbing

mocking-stubbing is a skill for isolating code in tests by replacing dependencies with controlled test doubles. It includes guidance and examples for JavaScript/TypeScript, Python, and Java testing.

Key features

  1. 01Replace external dependencies with test doubles
  2. 02Verify interactions between objects
  3. 03Simulate errors and edge cases
  4. 04Guides for Jest, unittest.mock, and Mockito
  5. 05Best practices for mock setup and reuse

Use cases

  1. 01Testing services without database or network calls
  2. 02Covering failure paths without real failures
  3. 03Testing behavior involving time or randomness
  4. 04Avoiding slow or costly operations in unit tests

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill mocking-stubbing

After installing, type/mocking-stubbingin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

references/
advanced-mocking-patterns.md
jest-mocking-javascripttypescript.md
mockito-for-java.md
python-mocking-with-unittestmock.md
scripts/
scaffold-tests.sh
templates/
test-template.js

Details

From the project README

Mocking and Stubbing

Table of Contents

Overview

Mocking and stubbing are essential techniques for isolating units of code during testing by replacing dependencies with controlled test doubles. This enables fast, reliable, and focused unit tests that don't depend on external systems like databases, APIs, or file systems.

When to Use

  • Isolating unit tests from external dependencies
  • Testing code that depends on slow operations (DB, network)
  • Simulating error conditions and edge cases
  • Verifying interactions between objects
  • Testing code with non-deterministic behavior (time, randomness)
  • Avoiding expensive operations in tests
  • Testing error handling without triggering real failures

Quick Start

Minimal working example:

// services/UserService.ts
import { UserRepository } from "./UserRepository";
import { EmailService } from "./EmailService";

export class UserService {
  constructor(
    private userRepository: UserRepository,
    private emailService: EmailService,
  ) {}

  async createUser(userData: CreateUserDto) {
    const user = await this.userRepository.create(userData);
    await this.emailService.sendWelcomeEmail(user.email, user.name);
    return user;
  }

  async getUserStats(userId: string) {
    const user = await this.userRepository.findById(userId);
    if (!user) throw new Error("User not found");

    const orderCount = await this.userRepository.getOrderCount(userId);
    return { ...user, orderCount };
  }
}

// ... (see reference guides for full implementation)

Reference Guides

Detailed implementations in the references/ directory:

GuideContents
Jest Mocking (JavaScript/TypeScript)Jest Mocking (JavaScript/TypeScript)
Python Mocking with unittest.mockPython Mocking with unittest.mock
Mockito for JavaMockito for Java
Advanced Mocking PatternsAdvanced Mocking Patterns

Best Practices

✅ DO
  • Mock external dependencies (DB, API, file system)
  • Use dependency injection for easier mocking
  • Verify important interactions with mocks
  • Reset mocks between tests
  • Mock at the boundary (repositories, services)
  • Use spies for partial mocking when needed
  • Create reusable mock factories
  • Test both success and failure scenarios
❌ DON'T
  • Mock everything (don't mock what you own)
  • Over-specify mock interactions
  • Use mocks in integration tests
  • Mock simple utility functions
  • Create complex mock hierarchies
  • Forget to verify mock calls
  • Share mocks between tests
  • Mock just to make tests pass

Comments

Markdown supported