Allow string arrays as parameters in StepDefinitionBaseAttribute
My company deals with payments. I use money-related amounts as variables in a lot of my step definitions.
The regex that I use is:
(-?[\d\.]+ \w{3})
This allows me to pass in a value such as "-95.00 USD", and then I have a StepArgumentTransformation method parse this into a proprietary type for use in my code.
One thing I find a bit annoying is that I need to paste this regex string all over the place in my steps.
If I try to use a string const instead of duplicating the regex string, I encounter the C# error
An attribute argument must be a constant expression, 'typeof()' expression or array creation expression of an attribute parameter type
One way I could circumvent this is if the attribute constructors accepted string arrays as an overload. So then, my step definition could be something like
[Given(new[] { "an amount of ", MoneyRegex, " when it's raining outside")]
Then, I would not have this duplicated regex pattern scattered all over my steps.
Thank you!
-
Update: This works as I need it to, but the Roslyn analyzer does not detect a match between the feature file and the regex in the step definition:
public class GivenListAttribute : GivenAttribute
{
public GivenListAttribute()
: this(null)
{
}
public GivenListAttribute(string[] regex)
: base(string.Join(string.Empty, regex))
{
}
}0 -
Hi David Fruhling,
Great suggestion and this has been on my mind also, we have something for this under https://support.specflow.org/hc/en-us/community/posts/360012169678--SpecFlow-Add-Cucumber-Expressions so you can definitely up vote this one to help move it along faster.
My only other suggestion is, with SpecFlow being open source, you could contribute to the project and implement this.
You can find the repo here - https://github.com/SpecFlowOSS/SpecFlow
many thanks for the feedback.
0
Please sign in to leave a comment.
Comments
2 comments