A restaurant menu scenario might list pizza names and prices in a table. If a later scenario needs calories, adding a calories column should not force every menu scenario to supply one. The Data Table Accessor pattern converts table rows into typed data objects while allowing columns that are irrelevant to a particular scenario to be omitted. section 14.5 section 14.0
Keep the conversion separate from the step’s business action. Instead of repeatedly reading cells by column name and parsing their values inside a step definition, convert each row into a menu-item object with properties such as name, price, calories, and ingredients. The step can then work with those objects. Optional properties remain unset when their columns are absent; the step can use an appropriate default where the action requires a value. section 14.5 section 14.8
Choose a conversion boundary that keeps the step readable:
- A dedicated conversion method can check which columns exist, parse supplied values, and return a list of typed objects. section 14.5
- A data-table helper such as CreateSet<MenuItemData>() can perform the row-to-object conversion. section 14.5
- A custom parameter type can move conversion out of the step definition altogether, so the step receives the converted list. section 14.5
Listing 14.24 Step definition uses built-in data table conversion
*** Support/CustomParameterTypes.cs ***
[StepArgumentTransformation] #A
public List<MenuItemData> ConvertMenuItemDataList(DataTable dataTable)
=> menuItemsTable.CreateSet<MenuItemData>().ToList();
*** StepDefinitions/MenuAdminStepDefinitions.cs ***
[Given("the restaurant menu is")]
public void GivenTheRestaurantMenuIs(
List<MenuItemData> menuItems) #B
{
foreach (var menuItem in menuItems)
{
MenuService.AddPizzaItem(
menuItem.Name,
manuItem.Price ?? DomainDefaults.MenuItemPrice);
}
}The transformation converts the table to a list of menu-item objects before the step runs; the step then uses those objects to populate the menu. section 14.5
The point is not to hide the menu data. It is to let a scenario show the data that matters to its behavior without making its automation depend on every possible column. Use a data structure designed for the table, handle missing properties deliberately, and keep cell-level parsing out of business-facing steps. section 14.8 section 14.0
