Default definitions

I start in the world of SpecFlow, and I ran into the first problem. Regarding my DRY code, I would like to do the following:

There are two scenarios:

Given I am on a product page
And myfield equals todays date
Then...

Given I am on a product page
And myfield equals todays date plus 4 days
Then...

I was hoping to use the following step definition for both variations of my proposal:

[Given(@"myfield equals todays date(?: (plus|minus) (\d+) days)?")]
public void MyfieldEqualsTodaysDate(string direction, int? days)
{
//do stuff
}

However, I keep getting exceptions when SpecFlow tries to parse int? couples. I checked the regex and it definitely parses the script as expected. I know that I could be as rude as method overloading, etc. I was just wondering if SpecFlow supports the idea of ​​default parameter values ​​or even another way to achieve the same effect.

Many thanks

+6
testing bdd specflow
source share
4 answers

Default values ​​are not supported (yet), but for your specific case I can suggest the following:

SpecFlow supports the creation of "step argument conversion". With their help, you can create methods that can analyze the date and time from different templates:

 [StepArgumentTransformation("todays date")] public DateTime TransformToday() { return DateTime.Today; } [StepArgumentTransformation("todays date (plus|minus) (\d+) days")] public DateTime TransformOtherDay(string direction, int days) { //... } 

After that, you just need to use the DateTime parameter in your steps, and the rest with SpecFlow ...

 [Given(@"myfield equals (.*)")] public void MyfieldEqualsTodaysDate(DateTime date) { //do stuff } 

you can see more examples at https://github.com/techtalk/SpecFlow/wiki/Step-Argument-Conversions

+8
source share

My friend uses the following technique

Given I am on a product page And myfield equals {TODAY}

Given I am on a product page And myfield equals {TODAY+4}

Then you can parse the special phrase in the defs step

[Given(@"myfield equals ("SOME MATCHING REGEX")]
public void MyfieldEqualsTodaysDate(string date) {
//parse TODAY or you could use TOMORROW you get the idea
}

+2
source share

Your steps seem to be formulated in a fairly developer-oriented language.

What happens if you pronounce them instead in the language of the parties concerned?

 Given I am on the product page And my product is due for delivery today Given I am on the product page And my product is due for delivery in 4 days Given I am on the product page And my product was due for delivery 3 days ago 

You can now use regexp to match these various steps and remove duplication at a lower level.

 [Given(@"my product is due for delivery today")] public void GivenTheProductIsDueToday() { var dueDate = Date.Today; DoOtherStuffWith(dueDate); } [Given(@"my product is due for delivery in (.*) days")] public void GivenTheProductIsDueIn(int days) { var dueDate = Date.Today.AddDays(days); DoOtherStuffWith(dueDate); } [Given(@"my product was due for delivery (.*) days ago")] public void GivenTheProductWasDue(int days) { var dueDate = Date.Today.AddDays(-1*days); DoOtherStuffWith(dueDate); } 

I am not using SpecFlow yet, but hope this makes sense. BDD focuses on conversations between business and stakeholders, rather than testing or automation. A compromise on what could be beneficial for DRY in the long run.

+2
source share

So far, the best I've managed to find is:
[Given(@"myfield equals todays date(?: ([\+-]\d+) days)?")]
public void MyfieldEqualsTodaysDate(string days)
{
int modifer = 0;
if(!String.IsNullOrEmpty(days))
{
modifer = Int32.Parse(days)
}
}

This is much cleaner than my original suggestion, but still requires me to manually check the parameter. Pay attention to the string parameter in the method. neither int nor int? work in the above scenarios.

0
source share

All Articles