Create and manage mocks, stubs, spies, and test doubles for isolating unit tests from external dependencies. Use for mock, stub, spy, test double, Mockito, Jest mocks, and dependency isolation.
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.
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)
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 |
Copy a source-pinned command for your client. You run it yourself.
Destination: .claude/skills/mocking-stubbing · pinned to the source commit
git clone https://github.com/aj-geddes/useful-ai-prompts.git
cd useful-ai-prompts
git checkout 3f5182cfd739fc113f4af5244a1cf342ad7f7911
mkdir -p ".claude/skills/mocking-stubbing"
cp -r "skills/mocking-stubbing" ".claude/skills/mocking-stubbing"Review the source before running. This copies files into your project; it is not a one-click install and does not verify runtime safety.
Scanner static-checks@0.1.0 · commit 3f5182cfd739. Static checks cannot prove runtime safety – review the source and the exact diff before installing. How checks work.
No static rules matched. This is not a safety guarantee.