Loading Depth Study
Preparing your workspace
Test doubles replace or observe real dependencies so tests can focus on the behavior being verified.
Applications often depend on databases, APIs, email providers, payment services, or other external systems. Unit tests should not normally call these real systems. A stub provides controlled return values, a mock can define expected interactions, and a spy records how a function was called. Test doubles should be used carefully because excessive mocking can make tests reflect implementation details instead of real behavior.
A course service can use a fake repository during a unit test instead of connecting to a real PostgreSQL database.
const repository = {
findById: vi.fn().mockResolvedValue({
id: 'c1',
title: 'React'
})
};
const course = await getCourse(repository, 'c1');
expect(repository.findById).toHaveBeenCalledWith('c1');
expect(course.title).toBe('React');The test verifies both the returned course and the repository call without using a real database.