liveBook · Manning

Default instance

As explained in Effective Behavior-Driven Development

An object created with agreed default values that can be customized for a scenario.

A default instance is a new object built from agreed default values that a scenario can customize. For example, a pizza item can start with a default name, size, and style; a step definition can then change only the name it needs. section 14.2

A practical pattern:

  • Keep agreed values in domain defaults, use a factory method to create a fresh instance, and override scenario-specific properties as needed. For complex domains, build larger default instances from lower-level ones. section 14.2
Listing 14.6 Step definitions that create pizza items using the generator method
*** Support/DomainDefaults.cs ***
public static class DomainDefaults 
{
  public const string PizzaName = "Margherita";
  public const string PizzaSize = "Medium";
  public const PizzaStyle PizzaStyle = PizzaStyle.Regular;

  public static PizzaItem PizzaItemDefaultInstance()    #A
  {
    return new PizzaItem(PizzaName, PizzaSize, PizzaStyle);
  }
}

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

[When("the customer places an order for {int} pizza(s) of size {string}")]
public void WhenTheCustomerPlacesAnOrderFor(int count, string pizzaSize)
{
  ... 
  for ( /* count */ ) 
  {
    var pizzaItem = 
      DomainDefaults.PizzaItemDefaultInstance();    #B
    pizzaItem.Size = pizzaSize;    #C
    order.AddItem(pizzaItem); 
  }
  ...
}

[Given("the customer has placed an order")]
public void GivenTheCustomerHasPlacedAnOrder()
{
  ... 
  var pizzaItem = 
    DomainDefaults.PizzaItemDefaultInstance();    #B
  order.AddItem(pizzaItem); 
  ...
}

See the C# factory method and its use in a step definition to customize a default pizza item. section 14.2

How Effective Behavior-Driven Development covers it

How deep it goes
Explains factory-created mutable objects, scalar defaults, and nested structures assembled from lower-level defaults.
Examples
Uses a customizable pizza item and describes orders containing multiple default order items.
What it assumes
Teaches factory methods from zero and assumes example objects have properties that can be customized.
What you can do after
Apply default instances by creating a fresh default object, then overriding only scenario-specific properties.
How it connects
Connects default instances with factory methods, domain defaults, test data, and step definitions.

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.