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.
In more detail
A spy can wrap the real function and let it run while it watches, or answer with canned values like a stub. Tests then assert on things such as how many times it was called and with what arguments. Use one when the call itself is the behaviour you care about, such as one email sent or one charge requested.
Example
assert spy.calls == [(1000, "abc")]Related terms
- mock — A test double that stands in for a real dependency and checks that the code under test calls it in the expected way.
- unit test — An automated check of one small piece of code, such as a single function, that runs quickly and on its own.
- 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.
- stub — A stand-in for a dependency in a test that simply returns prepared answers, without checking how it was called.
Taught in
- Native Test Runner: node:test & node:assert/strict — Node.js Internals & Backend Services
- Fakes, Stubs, Mocks And Spies — Automated Testing, TDD & Quality Engineering