liveBook · Manning

Stub dependency pattern

As explained in Effective Behavior-Driven Development

A testing pattern that replaces a slow or hard-to-control dependency with a controllable test substitute.

A delivery-delay scenario expects a notification when an order due at 18:45 has still not arrived by 18:46. Waiting for a real external time service would make that behavior awkward to test. A stub dependency gives the scenario a controllable version of the service instead. section 15.7

The stub dependency pattern replaces a slow or hard-to-influence dependency with a test version. The application still asks for the service it needs; the test can put that service into the state the scenario requires. This lets the scenario focus on the business outcome rather than on waiting for its environment. section 15.7 section 15.9

For the delivery scenario, the pieces work together:

  • A stub time service holds the current time and notifies subscribers when the test changes it. section 15.7
  • A time-service driver lets the step definition set the time named in the scenario. section 15.7
  • Application-host configuration registers that stub as the application's time service, so the application and scenario use the same controllable dependency. 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 stores the time and triggers subscribed callbacks when the test sets a new value. section 15.7

Listing 15.25 Configure application to use stub time service
*** Support/Hooks.cs ***
[Binding]
public class Hooks(StubTimeService stubTimeService, 
    AppHostingContext appHostingContext)
{
  [BeforeScenario]
  public void CreateAppHost()
  {
    appHostingContext.AppHost =    #A
      new WimpAppHost(stubTimeService);
  }
  ...
}
*** Support/WimpAppHost.cs ***
public class WimpAppHost(StubTimeService timeService) : ...
{
  protected override void ConfigureWebHost(IWebHostBuilder builder)
  {
    base.ConfigureWebHost(builder);
    ...    #B

    builder.ConfigureServices(diConfig =>    #C
    {
      diConfig.AddSingleton<ITimeService>(timeService);
    });
  }
}

The host registers the scenario's stub as ITimeService for the application. section 15.7

A stub can also make developer and CI builds faster or more reliable. Keep running a build against the real dependency regularly—for example, nightly—so differences between the stub and the real service do not produce false confidence. section 15.7

How Effective Behavior-Driven Development covers it

How deep it goes
Works through a stub service, driver, and host registration; defers fuller treatment of application-initialization hooks.
Examples
Uses a delivery-delay notification scenario and a controllable time service set to the scenario's required time.
What it assumes
Uses scenarios, step definitions, drivers, and dependency injection without teaching those mechanisms from zero.
What you can do after
Build it from scratch by creating a controllable service and registering it for scenarios, while checking regularly against the real dependency.
How it connects
Connects test drivers, dependency injection, scenario context, and Hexagonal Architecture to replaceable dependencies.

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.