Mocking and Stubbing

Mocking and Stubbing

Guides an AI coding agent to create mocks, stubs, spies and test doubles that isolate unit tests from databases, networks and other external dependencies.

Skill files7 files

references/

scripts/

templates/

SKILL.md100 lines
1
---
2
name: mocking-stubbing
3
description: >
4
Create and manage mocks, stubs, spies, and test doubles for isolating unit
5
tests from external dependencies. Use for mock, stub, spy, test double,
6
Mockito, Jest mocks, and dependency isolation.
7
---
8
9
# Mocking and Stubbing
10
11
## Table of Contents
12
13
- [Overview](#overview)
14
- [When to Use](#when-to-use)
15
- [Quick Start](#quick-start)
16
- [Reference Guides](#reference-guides)
17
- [Best Practices](#best-practices)
18
19
## Overview
20
21
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.
22
23
## When to Use
24
25
- Isolating unit tests from external dependencies
26
- Testing code that depends on slow operations (DB, network)
27
- Simulating error conditions and edge cases
28
- Verifying interactions between objects
29
- Testing code with non-deterministic behavior (time, randomness)
30
- Avoiding expensive operations in tests
31
- Testing error handling without triggering real failures
32
33
## Quick Start
34
35
Minimal working example:
36
37
```typescript
38
// services/UserService.ts
39
import { UserRepository } from "./UserRepository";
40
import { EmailService } from "./EmailService";
41
42
export class UserService {
43
constructor(
44
private userRepository: UserRepository,
45
private emailService: EmailService,
46
) {}
47
48
async createUser(userData: CreateUserDto) {
49
const user = await this.userRepository.create(userData);
50
await this.emailService.sendWelcomeEmail(user.email, user.name);
51
return user;
52
}
53
54
async getUserStats(userId: string) {
55
const user = await this.userRepository.findById(userId);
56
if (!user) throw new Error("User not found");
57
58
const orderCount = await this.userRepository.getOrderCount(userId);
59
return { ...user, orderCount };
60
}
61
}
62
63
// ... (see reference guides for full implementation)
64
```
65
66
## Reference Guides
67
68
Detailed implementations in the `references/` directory:
69
70
| Guide | Contents |
71
|---|---|
72
| [Jest Mocking (JavaScript/TypeScript)](references/jest-mocking-javascripttypescript.md) | Jest Mocking (JavaScript/TypeScript) |
73
| [Python Mocking with unittest.mock](references/python-mocking-with-unittestmock.md) | Python Mocking with unittest.mock |
74
| [Mockito for Java](references/mockito-for-java.md) | Mockito for Java |
75
| [Advanced Mocking Patterns](references/advanced-mocking-patterns.md) | Advanced Mocking Patterns |
76
77
## Best Practices
78
79
### ✅ DO
80
81
- Mock external dependencies (DB, API, file system)
82
- Use dependency injection for easier mocking
83
- Verify important interactions with mocks
84
- Reset mocks between tests
85
- Mock at the boundary (repositories, services)
86
- Use spies for partial mocking when needed
87
- Create reusable mock factories
88
- Test both success and failure scenarios
89
90
### ❌ DON'T
91
92
- Mock everything (don't mock what you own)
93
- Over-specify mock interactions
94
- Use mocks in integration tests
95
- Mock simple utility functions
96
- Create complex mock hierarchies
97
- Forget to verify mock calls
98
- Share mocks between tests
99
- Mock just to make tests pass
100

© AJ Geddes · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…