liveBook · Manning

StepArgumentTransformation attribute

As explained in Effective Behavior-Driven Development

A Reqnroll attribute that registers a method to transform captured text into a value for a step definition.

StepArgumentTransformation registers a method that converts text captured by a custom parameter placeholder into a value passed to a step definition. The method must be in a class marked with [Binding], and its attribute expression is a regular expression. section 12.3.4

When defining a transformation:

  • Use capture groups to pass matched text to the conversion method. section 12.3.4
  • Set Name to choose the custom parameter type’s name; if omitted, the return type name is used. section 12.3.4
  • Keep transformation methods in a separate support class when appropriate, rather than adding them to step-definition classes. section 12.3.4
Listing 12.2 Custom parameter type
[StepArgumentTransformation("(red|blue|yellow)", Name = "color")]
public Color ConvertColor(string colorValue)
{
    return new Color(colorValue);
}

A color-name expression captures the matched text and converts it to a Color value. section 12.3.4

Recognizing named orders

A transformation can extend an {order} parameter to recognize text such as “order B” and look up its order number. Step-definition matching occurs before the captured text is matched to a transformation. section 17.2

How Effective Behavior-Driven Development covers it

How deep it goes
Defines binding-class placement, regular-expression capture groups, and return-type-based naming, while deferring further examples.
Examples
Uses color-name conversion and named-order lookup to turn captured step text into Color and order-number values.
What you can do after
Build it with a binding-class method that matches captured text and returns the value required by the step definition.
How it connects
Connects custom parameter types and Cucumber Expressions to step definitions, matching the step before transforming captured text.

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.