Module 03: Mocking and Test Doubles¶
← Unit Testing | Topic Home | Next → Integration Testing
Mocks vs. stubs vs. fakes vs. spies vs. dummies — the five test double types; when to mock; the over-mocking trap; Python's unittest.mock and pytest-mock; Jest mocks.
Overview¶
This module is a stub. Content will be added in a future iteration.
Topics covered in this module:
- Gerard Meszaros's five test double types: dummy, stub, fake, spy, mock — precise definitions and when to use each
- Python's
unittest.mockin depth —MagicMock,patch,patch.object,side_effect,return_value - pytest-mock — cleaner mock API via the
mockerfixture - Jest mocks —
jest.fn(),jest.mock(),jest.spyOn(),mockReturnValue,mockImplementation - The over-mocking trap — when mocking makes tests brittle and meaningless
- Designing for testability — dependency injection patterns that make mocking easy
- Testing with time — freezing time in Python (freezegun) and JavaScript (jest.useFakeTimers)
- Testing randomness — seeding random number generators in tests
Prerequisites¶
- Module 02: Unit Testing in this topic
- Basic understanding of Python modules and JavaScript modules
Objectives¶
By the end of this module, you will be able to:
- Identify the correct test double type for any given scenario
- Use Python's
unittest.mock.patchto intercept and control dependencies - Use Jest's mock functions and module mocking to isolate JavaScript code
- Recognize over-mocking and refactor tests that are too tightly coupled to implementation
- Design code with dependency injection to make mocking natural
- Mock time and randomness to make tests deterministic
Cross-Links¶
- [[qa-testing/modules/02_unit-testing]] — Unit testing concepts that this module extends
- [[qa-testing/modules/04_integration-testing]] — Integration tests deliberately avoid mocking; understanding the contrast is important
- [[qa-testing/glossary#test-double]] — Formal definition of test doubles