在SpecFlow
问题描述:
同时传递表和单参数相同的功能我有一个阶跃函数,增加了一个数量计算器:在SpecFlow
: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 : Parameter count mismatch! The binding method 'SpecFlowFeature1Steps.GivenIHaveEnteredIntoTheCalculator(Int32)' should have 2 parameters
我可以把它用一个工作表中的唯一途径 - public void GivenIHaveEnteredIntoTheCalculator(Table table)
,但我想使用一个表,而无需重写功能。
答
从另一步调用一步。
首先,场景:
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);
}
}
使用[场景轮廓(https://github.com/cucumber/cucumber/wiki/Scenario-概述)进行第二次测试。在这种情况下,需要使用关键字“Scenario outline:”。 –
@JeroenMostert谢谢你,我查看了你提供的链接,但我认为我所寻找的内容稍有不同。我编辑了我的问题,它是否使事情更清楚? – sashoalm
是的,但在这种情况下,您确实需要某种形式的第二个功能,无法绕过它。您无法直接在SpecFlow /小黄瓜中用简写表达多步操作,您需要调整语言。一个简单的解决方法是更改第二种情况下的语法(“鉴于我已经顺序地将''输入到计算器中),第二种方法采用”表“并简单地在循环中调用第一种方法。我可以说,读者也可以更清楚地说明这一点。这种委托助手方法可能非常有用。 –