我有一个步骤函数,可以为计算器添加一个数字:
private readonly List<int> _numbers = new List<int>();
...
[Given(@"I have entered (.*) into the calculator")]
public void GivenIHaveEnteredIntoTheCalculator(int p0)
{
_numbers.Add(p0);
}
我可以使用以下语法从功能文件中调用它:
Given I have entered 50 into the calculator
但我也希望使用表调用相同的函数,在这种情况下,应该为表的每一行调用一次函数:
@mytag
Scenario: Add multiple numbers
Given I have entered 15 into the calculator
Given I have entered <number> into the calculator
| number |
| 10 |
| 20 |
| 30 |
When I press add
Then the result should be 75 on the screen
哪一个应该相当于:
@mytag
Scenario: Add multiple numbers
Given I have entered 15 into the calculator
And I have entered 10 into the calculator
And I have entered 20 into the calculator
And I have entered 30 into the calculator
When I press add
Then the result should be 75 on the screen
也就是说,带有表的And
子句和没有表调用的Given
子句/重用相同的函数,只有带有表的子句多次调用它。同时,其他子句只被调用一次 - 每行不调用一次,我认为这与使用场景上下文不同。
TechTalk.SpecFlow.BindingException:参数计数不匹配!绑定方法&#39; SpecFlowFeature1Steps.GivenIHaveEnteredIntoTheCalculator(Int32)&#39;应该有2个参数
我能使它工作的唯一方法是使用表 - public void GivenIHaveEnteredIntoTheCalculator(Table table)
,但我想使用表而不必重写函数。
答案 0 :(得分:2)
从另一个步骤调出一步。
首先,场景:
Scenario: Add multiple numbers
Given I have entered the following numbers into the calculator:
| number |
| 15 |
| 10 |
| 20 |
| 30 |
When I press add
Then the result should be 75 on the screen
现在步骤定义:
[Given("^I have entered the following numbers into the calculator:")]
public void IHaveEnteredTheFollowingNumbersIntoTheCalculator(Table table)
{
var numbers = table.Rows.Select(row => int.Parse(row["number"]));
foreach (var number in numbers)
{
GivenIHaveEnteredIntoTheCalculator(number);
}
}