This handles test isolation by replacing dependencies with mocks, stubs, and spies so you can unit test without hitting real databases, APIs, or file systems. It covers Jest for JavaScript/TypeScript, unittest.mock for Python, and Mockito for Java, plus patterns for things like simulating errors and verifying interactions. The guidance on mocking at boundaries (repositories, services) rather than mocking everything you own is solid advice that will keep your tests maintainable. Best used when you need fast, deterministic tests and want to avoid the brittleness that comes from over-mocking internal implementation details.

skills.sh installs
432
GitHub stars
379
last updated
Aug 21, 2026

Install

$ npx -y skills add aj-geddes/useful-ai-prompts --skill mocking-stubbing --agent claude-code

Installs into .claude/skills of the current project.

Files

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,
  ) {}

Sponsors

Details

Repository
github.com/aj-geddes/useful-ai-prompts
Categories
Testing & QA, AI & Agent Building
First seen
Jun 3, 2026

More from aj-geddes/useful-ai-prompts

All 258
1manutej avatarunix-goto-developmentmanutej/luxor-claude-marketplaceExpert guidance for unix-goto shell navigation tool development, including architecture, 9-step feature workflow, testing (100% coverage), performance optimizat428skills.sh installs
2aj-geddes avatarsynthetic-monitoringaj-geddes/useful-ai-promptsImplement synthetic monitoring and automated testing to simulate user behavior and detect issues before users. Use when creating end-to-end test scenarios, moni427skills.sh installs
3rstackjs avatarmigrate-to-rstestrstackjs/agent-skillsMigrate Jest or Vitest projects to Rstest. Use when asked to move from Jest/Vitest to Rstest, replace `jest`/`vi` APIs with `@rstest/core`, translate config int427skills.sh installs
4secondsky avatarnuxt-productionsecondsky/claude-skills| Nuxt 4 production optimization: hydration, performance, testing with Vitest, deployment to Cloudflare/Vercel/Netlify, and v4 migration. Use when: debugging hy425skills.sh installs
5nuxt avatarcontributingnuxt/uiGuide for contributing to Nuxt UI. Provides component structure patterns, Tailwind Variants theming, Vitest testing conventions, and MDC documentation guideline400skills.sh installs
6promptfoo avatarpromptfoo-evalspromptfoo/promptfooWrite, refine, run, and QA promptfoo evaluation suites: promptfooconfig.yaml, prompts, providers, vars, tests, assertions, model-graded rubrics, transforms, dat393skills.sh installs