liveBook · Manning

Domain default pattern

As explained in Effective Behavior-Driven Development

A pattern for defining default test data in a shared location to promote consistency and avoid maintenance issues from scattered hard-coded constants.

A scenario says a customer orders a Margherita pizza to qualify for a coupon. The pizza’s size does not affect that behavior, so the scenario leaves it out. Automation still needs a size to place the order. If each step definition supplies its own hard-coded value, those choices can drift apart and become difficult to change. section 14.1 section 14.8

The Domain Default pattern puts agreed default test values in one shared, readable location. A step definition uses the named default instead of embedding a value such as "Medium". This keeps incidental test data out of the scenario while making the automation’s choice visible and consistent. section 14.1 section 14.0

Listing 14.3 Class to keep the defaults and modified step definition
*** Support/DomainDefaults.cs ***
public static class DomainDefaults 
{
  public const string PizzaSize = "Medium"; #A
}

*** StepDefinitions/OrderingStepDefinitions.cs ***
[Given("the customer has placed an order containing a {string} pizza")]
public void GivenTheCustomerHasPlacedAnOrderContaining(string pizzaName)
{
  var order = ... 
  order.AddItem(pizzaName, DomainDefaults.PizzaSize);    #B
  ...
}

The step definition uses DomainDefaults.PizzaSize instead of repeating "Medium". The shared name shows where to find or change the default. section 14.1

Choose defaults deliberately:

  • Agree on values as a team, especially where business rules make some combinations invalid. section 14.1
  • Keep decisions in the readable defaults code rather than relying on a separate document that can become outdated; add a code comment when a decision needs more explanation. section 14.1
  • Introduce the shared location when the first default is needed, rather than waiting for hard-coded values to spread. section 14.1

A domain default supplies an incidental value; it does not make that value a rule of every scenario. Keep behaviorally important details explicit, and use shared defaults for the data needed to set up the rest. section 14.1 section 14.0

How Effective Behavior-Driven Development covers it

How deep it goes
Works through a pizza-ordering step definition and a shared defaults class; does not develop a larger default-object construction procedure.
Examples
Contrasts a hard-coded "Medium" pizza size with DomainDefaults.PizzaSize in a Margherita coupon scenario.
What it assumes
Assumes familiarity with scenarios and step definitions; introduces the shared-default pattern through a concrete ordering example.
What you can do after
Build it from scratch by agreeing on defaults, defining them in one readable location, and referencing them from step definitions.
How it connects
Links incidental scenario data to the essential principle, and distinguishes shared values from Default Instance factory methods.

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.