问题描述
相当新的SpecFlow如此容忍我。
Fairly new to SpecFlow so bear with me.
我和一位同事努力让你可以用SpecFlow做一个基本的了解。
I was working with a colleague to get a basic understanding of what you can do with SpecFlow.
我们分别采用了经典的FizzBuzz问题,这是我们用来测试的单元测试来比较,我们会怎么做SpecFlow类似的问题。
We were using the classic FizzBuzz problem, which we have used to test unit testing to compare how we would do a similar problem in SpecFlow.
我们写我们的方案如下代码越来越需要:
we wrote our scenarios as follows growing the code as needed:
(请原谅命名只是想获得测试令状)
(please excuse naming just wanted to get the tests writ)
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
这导致进化发展,将计算方法一些数字的总和。
This lead to an evolution to develop a method that would calculate a sum of some numbers
我们写的情况是:
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
我们写了接受一个的方法。在同一时间数再总结
The method we wrote accepted one number at a time to then sum up.
理想我将提供:
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 []
的方法签名。
How can you go about setting up the statement so that you can expect a params int[]
on the method signature.
推荐答案
您的问题是支持真的很好由specflow一步绑定,如果你使用了 StepArgumentTransformation
。这就是为什么我喜欢specflow。
Your problem is supported really nicely by the specflow step bindings, if you use a StepArgumentTransformation
. This is why I love 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将允许你采取任何逗号分离的任何整数列表从现在开始步定义,并接受它作为一个数组参数。
The StepArgumentTransformation here will allow you to take any comma separated list of ints in any step definition from now on, and accept it as an Array parameter.
如果你想StepArgumentTransformations玩,使他们很好的和具体这是值得我们学习的几个正则表达式位。注意:我可以使用(\d +(?:,\d +)*)。
而不是 *
中在当绑定了。
It's worth learning a few regex bits if you want to play with StepArgumentTransformations, to make them nice and specific. Note I could have used (\d+(?:,\d+)*)
instead of .*
in the When binding too.
这篇关于提供多个当SpecFlow方案陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!