Search code examples
c#.netregexbddspecflow

Dealing with dates when using Specflow


1

I have a Specflow feature like this:

Given A date of 1,1,2018

When I use "Generate step definitions" a method is produced like this:

[Given(@"A date of (.*),(.*)")]
public void GivenADateOf(Decimal p0, int p1)
{
    ScenarioContext.Current.Pending();
}

How do I change the method to accept three parameters? i.e.

public void GivenADateOf(int p0, int p1, int p2)
{
    ScenarioContext.Current.Pending();
}

2

Also say I wanted to change the feature to this:

Given A date of 1/1/2018

How do I change the method to accept one parameter? i.e.

public void GivenADateOf(datetime p0)
{
    ScenarioContext.Current.Pending();
}

I am new to Specflow. I have looked here: How does specflow handle multiple parameters? among other places.


Solution

  • SpecFlow allows to specify different regular expressions for catching arguments.

    For the first option I would use:

    [Given(@"A date of ([0-9]*),([0-9]*),([0-9]*)")]
    public void GivenADateOf(int day, int month, int year)
    {
       ScenarioContext.Current.Pending();
    }
    

    For the second option I would use regular expression that corresponds to your date format. For example:

    [Given(@"A date of (.*)")]
    public void GivenADateOf(DataTime dataTime)
    {
         ScenarioContext.Current.Pending();
    }