I am trying to write a StepArgumentTransformation for specflow.
I have following gherkin
Scenario: Test Arguments
Given user enter once as 2
And I have written this in step definition.
[StepArgumentTransformation]
public int GetOnces(string onces, string times)
{
return 1 * int.Parse(times);
}
[Given(@"user enter (.*) as (.*)")]
public void GivenUserEnterOnce(int num)
{
Assert.Equal(2, num);
}
But GetOnces method is never called and I get the exception
TechTalk.SpecFlow.BindingException: 'Parameter count mismatch! The binding method 'GivenUserEnterOnce(Int32)' should have 2 parameters
The bindings should be like this:
[StepArgumentTransformation("(.*) as (.*)")]
public int GetOnces(string onces, string times)
{
return 1 * int.Parse(times);
}
[Given(@"user enter (.*)")]
public void GivenUserEnterOnce(int num)
{
Assert.Equal(2, num);
}
If it's more than one parameter you want to transform, you have to specify the regex at the StepArgumentTransformation. Your real step only get's one parameter, so only one parameter in the regex is valid.
You find the documentation for this here: https://specflow.org/documentation/Step-Argument-Conversions/