stub
A stand-in for a dependency in a test that simply returns prepared answers, without checking how it was called.
In more detail
Use a stub when you only need data for a local rule, such as feeding a parser a malformed response. Unlike a mock, it does not verify how the code used it.
Examples
In this course: Use a stub when the test's claim is about the answer your code produces. Hand-written stubs keep you honest about what the real collaborator returns. A mock whose calls nobody checks is really a stub.class StubGateway:
def charge(self, pence, reference):
return {"reference": reference}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.
- 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.
- 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.
- 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.
Taught in
- Mocking Dependencies — Modern API Architecture: REST, GraphQL & gRPC
- What Test Doubles Are For — Automated Testing, TDD & Quality Engineering
- Fakes, Stubs, Mocks And Spies — Automated Testing, TDD & Quality Engineering