liveBook · Manning

Stub

As explained in Effective Behavior-Driven Development

A test double that substitutes for a real dependency and provides predetermined behavior or responses.

A stub is a test-friendly substitute for a dependency. It lets a scenario give the application a controlled response instead of relying on a real external service. This is an automation choice, not a requirement of BDD: the team can use real services or substitutes according to its strategy. section 12.5 section 15.7

A delivery that is late at 18:46

A customer expects a delivery at 18:45 and should receive a delay notification if it has not arrived by 18:46. Waiting for the clock to reach 18:46 would make the test slow. Instead, a stub time service lets the scenario set the time to 18:46 and trigger the time-change event. The application can then respond as though that time had arrived. section 15.7

The application and the stub use the same time-service interface. The stub returns its controlled current time and notifies subscribers when that time changes. A driver translates the scenario’s business-readable step into a call that sets the time; scenario setup configures the application to use the stub. Keeping those details behind the step preserves the scenario’s focus on the customer-facing behavior. section 15.7

Listing 15.23 Stub time service implementation
*** Support/StubTimeService.cs ***
public class StubTimeService : ITimeService
{
  private DateTimeOffset now = DateTimeOffset.Now;
  private readonly List<Func<DateTimeOffset, bool>> timeChangeSubscribers = 
      [];

  public DateTimeOffset GetCurrentTime() => now;

  public void SubscribeToTimeChange(
      Func<DateTimeOffset, bool> onTimeChanged) =>
    timeChangeSubscribers.Add(onTimeChanged);

  public void SetCurrentTime(DateTimeOffset currentDateTime)
  {
    now = currentDateTime;
    TriggerTimeChange();
  }

  public void TriggerTimeChange()
  {
    var currentDateTime = GetCurrentTime();
    foreach (var subscriber in timeChangeSubscribers.ToArray())
    {
      if (subscriber(currentDateTime))
      {
        timeChangeSubscribers.Remove(subscriber);
      }
    }
  }
}

The stub time-service implementation shows both halves of the substitution: returning a settable time and triggering subscribed time-change callbacks. section 15.7

The same choice can apply to other external dependencies, including a database or payment provider. In a hexagonal architecture, an outbound port gives tests a place to connect either a real service adapter or a stub. Favor the arrangement that gives useful, fast, robust feedback for the scenario; the choice need not be identical for every dependency. section 15.7 section 15.8.1

How Effective Behavior-Driven Development covers it

How deep it goes
Works through a settable time service, event callbacks, driver, and application configuration; leaves the general real-versus-stub decision open.
Examples
Uses a delivery-delay notification at 18:46, a stub time service, and configuration that installs it for a scenario.
What it assumes
Assumes familiarity with scenarios and an application dependency exposed through an interface; illustrates the stub through a concrete time-based case.
What you can do after
Build it from scratch as a replaceable time service that supplies controlled time and triggers time-change events in a scenario.
How it connects
Connects to BDD scenario automation, external services, dependency injection, drivers, and hexagonal architecture’s outbound ports and adapters.

Also explained in

Compare how 14 books explain stub →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Effective Behavior-Driven Development. The section links point to the parts of the book it draws on.