liveBook · Manning

Object mother

As explained in Effective Behavior-Driven Development

A test-data construction pattern that provides methods for creating default objects or object hierarchies customized for particular tests.

A test scenario may need an order with a particular pizza, several large pizzas, or just an ordinary order. Rebuilding each order in its step definition repeats incidental details such as the delivery address. An object mother creates a sensible default object, then accepts instructions for the differences that matter to the scenario. section 14.3

For a simple pizza item, the object mother can be one method: optional name, size, and style arguments override shared defaults before the item is constructed. Unlike changing properties on a returned default instance, this also works when those properties cannot be set afterward. section 14.3 section 14.8

An order with only the relevant difference

A default order has one default pizza and a default delivery address. To specify a named pizza, use `new OrderObjectMother().WithItem(name: pizzaName).Build()`. To specify several pizzas of one size, use `WithItems(count, size: pizzaSize)` instead. A scenario needing no changes simply calls `Build()`. section 14.3

Listing 14.10 Object mother class for building orders
*** Support/OrderObjectMother.cs ***
public class OrderObjectMother 
{
  private readonly Order order = new();

  public OrderObjectMother()    #A
  {
    // add default item
    order.AddItem(DomainDefaults.PizzaItemDefaultInstance());
    // add default address
    order.DeliveryAddress = DomainDefaults.DeliveryAddress;
  }

  public Order Build()    #B
  {
    return order;
  }

  public OrderObjectMother WithAdditionalItem(    #C
    string? name = null, string? size = null, PizzaStyle? style = null)
  {
    var pizzaItem = DomainDefaults
      .PizzaItemDefaultInstance(name, size, style);
    order.AddItem(pizzaItem);
    return this;
  }

  public OrderObjectMother WithItem(    #D
    string? name = null, string? size = null, PizzaStyle? style = null)
  {
    order.ClearItems(); // remove existing items
    return WithAdditionalItem(name, size, style);
  }

  public OrderObjectMother WithItems(int quantity,    #E
    string? name = null, string? size = null, PizzaStyle? style = null)
  {
    order.ClearItems(); // remove existing items
    for (int i = 0; i < quantity; i++)
      WithAdditionalItem(name, size, style);
    return this;
  }
}

The order builder starts with a default item and address; its customization methods change the order being assembled before `Build()` returns it. section 14.3

Keep the interface as small as current scenarios require. A single method may be enough for one item; an order hierarchy may justify a separate builder with readable, chainable instructions. Add customization methods as real needs emerge rather than designing every possible variation upfront. section 14.3

How Effective Behavior-Driven Development covers it

How deep it goes
Works through an optional-parameter method and a separate order builder, while declining a one-size-fits-all interface design.
Examples
Uses pizza items with selected names or sizes and orders with one default item or multiple customized items.
What it assumes
Builds on shared domain defaults and default-instance factories, then addresses cases where returned objects cannot be modified.
What you can do after
Build it from scratch as a small customization method or an order builder that returns scenario-ready test data.
How it connects
Connects domain defaults, default instances, the builder pattern, and fluent interfaces.

Also explained in

Compare how 2 books explain object mother →

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.