oslac

Search

Search IconIcon to open search

Stubs

Published: Jun 1, 2023
Updated: Jun 3, 2023

Info

A stub contains no logic; it only returns a predetermined value or an object (we stub the return value).

Use Case

Stubs can be used when you need an object to return specific values to get your code under test into a certain state. Since we define the function’s behavior inline within the test, stubbing allows us to simulate a wide variety of return values or errors that might not be possible to trigger from a real implementation or a fakes.

To ensure its purpose is clear, each stubbed function should have a direct relationship with the test’s assertions1. As a result, a test typically should stub out a small number of functions because stubbing out many functions can lead to tests that are less clear.

A mocking framework is often a convenient way to reduce boilerplate of writing stubs.

Issues with Stubbing


  1. Test Doubles by Andrew Trenk and Dillon Bly ↩︎