问题
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;
}
回答1:
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
回答2:
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.
Ho 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:
public int[] ConvertToIntArray(string argument)
{
argument.Split(",").Select(x=>Convert.ToInt32(x)).ToArray();
}
and you should then be able to define your setp 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.
回答3:
This is the best solution in my opinion:
When I add the following numbers
| Numbers|
| 5 |
| 1 |
| 3 |
And the step
[When(@"I add the following numbers")]
public static void AddUp(Table values)
{
//addition code
}
来源:https://stackoverflow.com/questions/31789172/how-does-specflow-handle-multiple-parameters