StepArgumentTransformations using Name parameter #974
-
|
I'm having a bit of trouble using the StepArgumentTransformation attribute with a named parameter and having it accurately refine to what I expected based on the expressions in my Given/When/Then clauses. From the docs here
I was expecting this to mean that the argument transform would only be executed for values in expressions whose names matched the transformation attribute, but it seems like this isn't the case: [Then("it should last {int} {timePeriod}", ExpressionType = ExpressionType.CucumberExpression)]
public void ThenItShouldLastIntTimePeriod(int numberOfPeriods, int timePeriodToDayMultiplier)
{
var totalDays = numberOfPeriods * timePeriodToDayMultiplier;
totalDays.Should().Be(_result);
}
[StepArgumentTransformation(Name = "timePeriod", Order = 0)]
public int ConvertToDayMultiplier(string timePeriod)
{
return timePeriod.ToLowerInvariant() switch
{
"day" or "days" => 1,
"week" or "weeks" => 7,
_ => throw new ArgumentException($"Unsupported time period: {timePeriod}")
};
}In this example, I'd use gherkin along the lines of "Then it should last 10 days", but what happens when I execute this test is that I end up with an error saying "Unsupported time period: 10", which seems to imply that the {int} value was passed to the StepArgumentTransform. Am I missing something? Have I misinterpreted this functionality and how it's expected to behave? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
|
Note: all typed below on mobile, so no IDE to check it. AFAIK you need the regex in the StepArgumentTransformation, something like this: [StepArgumentTransformation("(days?|weeks?)", ... Otherwise it means, convert any string to int. I think it would be logical if the StepArgumentTransformation In that case the regex would be "(\d+) (days?|weeks?)", and add the int parameter to the StepArgumentTransformation. Or even better, 2 StepArgumentTransformations. One for days, the other for weeks. Then the StepArgumentTransformation has one int parameter and no need to pattern match the string. I don't think you need the name property (correct me if I'm wrong) E.g. [StepArgumentTransformation(@"(\d+) days?")]
public int InXDaysTransform(int days)
{
return days;
}
[StepArgumentTransformation(@"(\d+) weeks?")]
public int InXWeeksTransform(int weeks)
{
return weeks * 7;
}
[Then("it should last {}", ExpressionType = ExpressionType.CucumberExpression)]
public void ThenItShouldLastIntTimePeriod(int totalDays)
{
totalDays.Should().Be(_result);
} |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the reply. I did try with the regex string as well, which does seem to work as you describe, but I guess I'd just ask what the purpose of the "Name" property on the transformation attribute is then? It seems to at least imply that the transformation is scoped explicitly to references from expressions which share that name, i.e. only matches Is the expectation that this should be the behaviour? In what contexts would it make sense for that transformation to be used for the |
Beta Was this translation helpful? Give feedback.
-
|
We should update the docs, PR would be great :) https://github.com/reqnroll/Reqnroll/edit/main/docs/automation/step-argument-conversions.md |
Beta Was this translation helpful? Give feedback.
The name is AFAIK only for clarity. But I'm not sure on that.