Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does specflow handle multiple parameters?

As the title says how does specflow handle this

x = AddUp(2, 3)
x = AddUp(5, 7, 8, 2)
x = AddUp(43, 545, 23, 656, 23, 64, 234, 44)

The link I gave is how it is usually done. What i want to know is what should the annotation on the top be?

[Then(@"What should I write here")]
public static void AddUp(params int[] values)
{
   int sum = 0;
   foreach (int value in values)
   {
      sum += value;
   }
   return sum;
}
like image 991
mosaad Avatar asked Sep 13 '25 23:09

mosaad


2 Answers

I don't believe that you can have a binding which uses a param array as the argument type in specflow and have it used automatically. At least I've never used one and have never seen one used.

How would you want to specify this in the feature file? you have given examples of the methods you want to call and the step definition method you want specflow to call, but not how you expect the feature file to read.

My suspicion is that you would want to do something like this

Given I want to add up the numbers 2,5,85,6,78

and ultimately specflow will want to convert this into a string and call a method. You'll have to do the conversion from string to array of numbers yourself, probably using a [StepArgumentTransformation] like this:

[StepArgumentTransformation]
public int[] ConvertToIntArray(string argument)
{
    argument.Split(",").Select(x=>Convert.ToInt32(x)).ToArray();
} 

and you should then be able to define your step definition like so:

[Given(@"I want to add up the numbers (.*)")]
public static void AddUp(params int[] values)
{
    int sum = 0;
    foreach (int value in values)
    {
        sum += value;
    }
    return sum;
}

but honestly at this point you don't need the params bit, int[] will be enough.

like image 91
Sam Holder Avatar answered Sep 15 '25 11:09

Sam Holder


You add parameters by adding single quote marks like this:

[When(@"I perform a simple search on '(.*)'")]
public void WhenIPerformASimpleSearchOn(string searchTerm)
{
    var controller = new CatalogController();
    actionResult = controller.Search(searchTerm);
}

you can use comma seperated lists

When i login to a site
then 'Joe,Bloggs,Peter,Mr,Some street,15' are valid

Also you can use table vales

When I login to a site
then the following values are valid
    | FirstName | LastName | MiddleName | Greeting| Etc    | Etc     |
    | Joe       | Bloggs   | Peter      | Mr      | you get| The Idea|

https://github.com/techtalk/SpecFlow/wiki/Step-Definitions Providing multiple When statements for SpecFlow Scenario Passing arrays of variable in specflow

like image 32
tdbeckett Avatar answered Sep 15 '25 11:09

tdbeckett