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

