为SpecFlow Scenario提供多个When语句

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

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

我们使用了经典的FizzBu​​zz问题,我们用它来测试unit testing,以比较我们如何在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 

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

我们写的场景是:

 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 

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

理想情况下我会提供:

 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 

你怎么能设置语句,以便你可以期望方法签名上的params int[]

如果使用StepArgumentTransformation ,则specflow步骤绑定非常好地支持您的问题。 这就是我喜欢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(); } 

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

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