mocking-stubbing

aj-geddes/useful-ai-prompts · updated Apr 8, 2026

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

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.

skill.md

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:

Guide Contents
Jest Mocking (JavaScript/TypeScript) Jest Mocking (JavaScript/TypeScript)
Python Mocking with unittest.mock Python Mocking with unittest.mock
Mockito for Java Mockito for Java
Advanced Mocking Patterns Advanced 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

Discussion

Product Hunt–style comments (not star reviews)
  • No comments yet — start the thread.
general reviews

Ratings

4.469 reviews
  • Chinedu Agarwal· Dec 8, 2024

    I recommend mocking-stubbing for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.

  • Ganesh Mohane· Dec 4, 2024

    Useful defaults in mocking-stubbing — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.

  • Yuki Martinez· Dec 4, 2024

    Solid pick for teams standardizing on skills: mocking-stubbing is focused, and the summary matches what you get after install.

  • Tariq Ndlovu· Dec 4, 2024

    Keeps context tight: mocking-stubbing is the kind of skill you can hand to a new teammate without a long onboarding doc.

  • Zara Agarwal· Nov 27, 2024

    Solid pick for teams standardizing on skills: mocking-stubbing is focused, and the summary matches what you get after install.

  • Rahul Santra· Nov 23, 2024

    mocking-stubbing has been reliable in day-to-day use. Documentation quality is above average for community skills.

  • Kabir Haddad· Nov 23, 2024

    I recommend mocking-stubbing for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.

  • Kiara Okafor· Nov 23, 2024

    Registry listing for mocking-stubbing matched our evaluation — installs cleanly and behaves as described in the markdown.

  • Yusuf Verma· Nov 11, 2024

    Useful defaults in mocking-stubbing — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.

  • Dev Lopez· Nov 7, 2024

    We added mocking-stubbing from the explainx registry; install was straightforward and the SKILL.md answered most questions upfront.

showing 1-10 of 69

1 / 7