将 table 和单个参数传递给 SpecFlow 中的同一函数

Passing both table and single argument to same function in 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

但我还想使用 table 调用相同的函数,在这种情况下,应该为 table:

的每一行调用一次该函数
@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

即带table的And子句与不带table的Given子句call/reuse的功能相同,只是带的子句a table 多次调用它。同时,其他子句只被调用一次——而不是每行一次,我认为这与使用场景上下文不同。

但是,这不起作用。我收到以下错误:

TechTalk.SpecFlow.BindingException : Parameter count mismatch! The binding method 'SpecFlowFeature1Steps.GivenIHaveEnteredIntoTheCalculator(Int32)' should have 2 parameters

唯一能让它工作的方法是使用 table - public void GivenIHaveEnteredIntoTheCalculator(Table 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);
    }
}