对SpecFlow来说还挺新鲜的,所以请容忍我。
我与一位同事一起工作,以了解您可以使用SpecFlow做些什么。
我们使用了经典的FizzBuzz问题,我们用它来测试单元测试,以比较我们如何在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[]
。
发布于 2013-11-05 05:34:16
如果您使用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();
}
这里的StepArgumentTransformation将允许您从现在开始在任何步骤定义中接受任何逗号分隔的it列表,并接受它作为一个数组参数。
如果您想玩StepArgumentTransformations,那么值得学习几个regex位,以使它们更好、更具体。注意,我也可以在绑定中使用(\d+(?:,\d+)*)
而不是.*
。
https://stackoverflow.com/questions/19787187
复制