Question

For our SpecFlow scenarios, we need to intercept string arguments before they get to the step methods, since we are doing some custom processing of those values. I am trying to add a generic StepArgumentTransformation attribute, which I'm assuming should capture any string argument, but it's never getting hit:

[Binding]
public class Transforms
{
    [StepArgumentTransformation]
    public string StringTransform(string value)
    {
        // Transform code is here...
    }
}

The only thing I can think of is that it's not possible to just do a general string -> string transform?

Does anyone have any ideas? Thanks!

Was it helpful?

Solution

SpecFlow does an automatic conversion when it's a string and you can't intercept that. From the docs:

The following conversions can be performed by SpecFlow (in the following precedence):
 - no conversion, if the argument is an instance of the parameter type (e.g. the parameter type is object or string)
 - step argument transformation
 - standard conversion

A work-around would be to define a custom type with a single property to hold the custom string value:

public class CustomString
{
    public string Value {get; set;}
}

Now you can use the attribute to convert a string that matches a certain regex to this custom class.

More information: http://www.specflow.org/documentation/Step-Argument-Conversions/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top