mock
A test double that stands in for a real dependency and checks that the code under test calls it in the expected way.
In more detail
A mock records or expects specific calls, so the test depends on which methods are called and with which arguments. People often say mock for any test double, but strictly a mock is the kind that verifies interactions, so use one when the interaction really is the behaviour. Never mock the code under test, and pair mocks with contract or integration tests, because a mock is only an assumption about the real dependency.
Example
node:test provides mock.fn(), mock.method() and mock.timers; mocks created through the test context t.mock are restored automatically when that test ends.Related terms
- stub — A stand-in for a dependency in a test that simply returns prepared answers, without checking how it was called.
- test double — Any object that replaces a real dependency in a test: a stub returns canned data, a fake is a small working implementation, and a mock checks interactions.
- contract testing — Testing that an API’s real behaviour matches its written contract, and that its callers only rely on what the contract promises.
- integration test — A test that checks several real parts working together, such as the service talking to a real database.
- NSubstitute — A .NET library that creates test doubles for interfaces with readable syntax, such as Substitute.For<IProductRepository>().
- fake — A test double that is a small but working implementation of a dependency, such as an in-memory database standing in for a real one.
- test spy — A test double that records how a function was called, with its arguments and results, so a test can check afterwards how it was used.
- unit test — An automated check of one small piece of code, such as a single function, that runs quickly and on its own.
- test runner — A tool that finds test files, runs the tests in them and reports which passed and which failed.
- autospec — A unittest.mock feature that builds a mock with exactly the same methods and signatures as a real class.
- over-mocking — Replacing so many collaborators with mocks that tests only check the script the test writer invented, not real behaviour.
- unittest.mock — The mocking library in Python's standard library, which builds mock objects and records how they were called.
Taught in
- Unit And Integration Testing — Modern API Architecture: REST, GraphQL & gRPC
- Mocking Dependencies — Modern API Architecture: REST, GraphQL & gRPC
- Unit Testing with xUnit and NSubstitute — Modern .NET Core, C# & Enterprise Microservices
- Testing APIs With MockMvc — Modern Java: Virtual Threads & High-Throughput Services
- Database Integration With Testcontainers — Modern Java: Virtual Threads & High-Throughput Services
- Native Module Systems: ESM Loader Hooks vs CommonJS — Node.js Internals & Backend Services
- Native Test Runner: node:test & node:assert/strict — Node.js Internals & Backend Services
- The Test Pyramid And Where It Is Wrong — Automated Testing, TDD & Quality Engineering
- Fakes, Stubs, Mocks And Spies — Automated Testing, TDD & Quality Engineering
- Why Over-Mocking Breeds False Positives — Automated Testing, TDD & Quality Engineering