Optional parameters with Specflow

Two things pops into my head:

Firstly I now a days don't worry to much of having loads of step definitions as long as they are short (oneliners) and calling into some sort of automation layer or DSL that helps me to automate the system under test. See this excellent presentation for inspiration.

So in that sense you could just double the step definitions with "overloads" for each case.

My first stab on a second solution was to stack attributes on top of each others for the same method. That works, but not for optional parameters. (And optional parameters doesn't work to well with reference types as DateTime, but DateTime? nullable works). If you step up your steps like that you will get an error like this:

Parameter count mismatch! The binding method 'Class1.MyGiven(Int32, Int32, Int32)' should have 1 parameters

So I fall back to my first solution like this:

    [Given(@"case one with one int '(\d+)'")]
    public void Case1(int a)
    {
        // Short but sweet step definition that calls into automation layer
    }

    [Given(@"case one with two ints '(\d+)' and '(\d+)'")]
    public void Case2(int a, int b)
    {
        // Short but sweet step definition that calls into automation layer
    }

    [Given(@"case one with three ints '(\d+)' and '(\d+)' and also '(\d+)'")]
    public void Case3(int a, int b , int c)
    {
        // Short but sweet step definition that calls into automation layer
    }

I hope I didn't cause too much confusion by going back and forth. Sorry - had no IDE ready on the bus :)


I prefer the simplicity of multiple steps, but if you want do do what you propose, you will need to craft an appropriate regex. Something like (not tested):

[Given(@"(at ([0-9-]+) (really at ([0-9-]+) |)|)(\w+) Completed a transfer form to transfer \$(\d+) to account (\d+)"]
public void TransferStep(string dummy1, DateTime? atDate, string dummy2, DateTime? reallyAtDate, string name, decimal amount, int account)

This worked well for me, overloading the functions that require optional parameters and passing in defaults.

It would be great if specflow supported optional parameters e.g. string searchTerm = "" but it doesn't at the moment.

[When(@"I request a list of managers")]
public void WhenIRequestAListOfManagers()
{
    WhenIRequestAListOfManagers("");
}

[When(@"I request a list of managers with the search term (.*)")]
public void WhenIRequestAListOfManagers(string searchTerm)
{
   //do stuff
}

Tags:

C#

Bdd

Specflow