为SpecFlow Scenario提供多个When语句

Luk*_*dge 7 c# params specflow

SpecFlow相当新,所以请耐心等待.

我正和一位同事一起基本了解你可以用SpecFlow做些什么.

我们使用了经典的FizzBu​​zz问题,我们用它来测试单元测试,以比较我们在SpecFlow中如何处理类似问题.

我们编写了我们的场景,如下所示增长代码:

(请原谅命名只是想获得测试令状)

Scenario: 1 is 1
    Given there is a FizzBuzzFactory
    When you ask What I Am with the value of 1
    Then the answer should be 1 on the screen

Scenario: 3 is Fizz
    Given there is a FizzBuzzFactory
    When you ask What I Am with the value of 3
    Then the answer should be Fizz on the screen

Scenario: 5 is Buzz
    Given there is a FizzBuzzFactory
    When you ask What I Am with the value of 5
    Then the answer should be Buzz on the screen

Scenario: 15 is FizzBuzz
    Given there is a FizzBuzzFactory
    When you ask What I Am with the value of 15
    Then the answer should be FizzBuzz on the screen
Run Code Online (Sandbox Code Playgroud)

这导致了开发一种计算某些数字之和的方法的演变

我们写的场景是:

Scenario: Sumof 1 + 2 + 3 is Fizz
    Given there is a FizzBuzzFactory
    When you add the sum of 1
    When you add the sum of 2
    When you add the sum of 3
    Then the answer should be Fizz on the screen
Run Code Online (Sandbox Code Playgroud)

我们写的方法一次接受一个数字然后总结.

理想情况下我会提供:

Scenario: Sumof 1 + 2 + 3 in one go is Fizz
    Given there is a FizzBuzzFactory
    When you add the sum of 1,2,3
    Then the answer should be Fizz on the screen
Run Code Online (Sandbox Code Playgroud)

如何设置语句,以便您可以期待params int[]方法签名.

per*_*ist 10

如果你使用一个,你的问题可以通过specflow步骤绑定得到很好的支持StepArgumentTransformation.这就是我喜欢specflow的原因.

[When(@"you add the sum of (.*)")]
public void WhenYouAddTheSumOf(int[] p1)
{
    ScenarioContext.Current.Pending();
}

[StepArgumentTransformation(@"(\d+(?:,\d+)*)")]
public int[] IntArray(string intCsv)
{
    return intCsv.Split(',').Select(int.Parse).ToArray();
}
Run Code Online (Sandbox Code Playgroud)

这里的StepArgumentTransformation允许您从现在开始在任何步骤定义中使用任何逗号分隔的int列表,并将其作为Array参数接受.

如果你想玩StepArgumentTransformations,那么值得学习一些正则表达式,以使它们变得美观和具体.注意我可以使用(\d+(?:,\d+)*)而不是.*在When绑定中.