Overview

A stub is a simple test double that returns a fixed value when a specific method is called. Unlike a mock, a stub does not typically verify how it was called; it only exists to provide the necessary data for the test to proceed.

Example

If you are testing a function that calculates a discount, you might 'stub' the database call that fetches the user's loyalty status to always return 'Gold'.

Benefits

  • Simplifies test setup.
  • Makes tests faster and more predictable by avoiding real external calls.

Related Terms