Behavioral-test steps sometimes need technical values that do not match the language a team uses to describe its work. The terminology promotion pattern uses custom parameter types to convert domain-specific terms centrally, keeping specifications readable and conversion logic maintainable. section 14.4
Pizza sizes
A scenario can specify a pizza size such as 14 inches. A custom parameter type maps that value to the corresponding PizzaSize enum, keeping the conversion in one place instead of embedding a manual mapping in the step definition. section 14.4
Listing 14.16 Define a pizza-size parameter type
*** Features/Ordering.feature ***
Rule: More than 4 large pizzas cannot be delivered as a single order
Scenario: Five large pizzas are ordered
...
When the customer places an order for 5 pizzas of size 14"
*** Support/CustomParameterTypes.cs ***
[StepArgumentTransformation("(10\"|12\"|14\")", Name = "pizza-size")]
public PizzaSize ConvertPizzaSize(string pizzaSizeString) =>
pizzaSizeString switch
{
"10\"" => PizzaSize.Small,
"12\"" => PizzaSize.Medium,
"14\"" => PizzaSize.Large,
_ => throw new FormatException($"Invalid size: {pizzaSizeString}")
};
*** StepDefinitions/OrderingStepDefinitions.cs ***
[When(
"the customer places an order for {int} pizza(s) of size {pizza-size}")]
public void WhenTheCustomerPlacesAnOrderFor(int count, PizzaSize pizzaSize)
{
var order = new OrderObjectMother()
.WithItems(count, size: pizzaSize)
.Build();
...
}See the C# example defining a pizza-size parameter type and using it in a step definition. section 14.4
