首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >为SpecFlow场景提供多个When语句

为SpecFlow场景提供多个When语句
EN

Stack Overflow用户
提问于 2013-11-05 10:49:17
回答 1查看 3.4K关注 0票数 7

对SpecFlow来说还挺新鲜的,所以请容忍我。

我与一位同事一起工作,以了解您可以使用SpecFlow做些什么。

我们使用了经典的FizzBuzz问题,我们用它来测试单元测试,以比较我们如何在SpecFlow中解决类似的问题。

我们编写了我们的场景,如下所示,根据需要编写代码:

(请原谅指名道姓只是为了拿到考试证书)

代码语言:javascript
代码运行次数:0
运行
复制
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

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

我们编写的场景是:

代码语言:javascript
代码运行次数:0
运行
复制
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

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

理想情况下,我会提供:

代码语言:javascript
代码运行次数:0
运行
复制
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[]

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-05 13:34:16

如果您使用StepArgumentTransformation,那么specflow步骤绑定很好地支持您的问题。这就是为什么我喜欢斑点流。

代码语言:javascript
代码运行次数:0
运行
复制
[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+)*)而不是.*

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19787187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档