如何在SpecFlow中创建功能以满足多种需求

问题描述

|| 我正在学习SpecFlow,并且正在做一个简单的Com-Sci标准FizzBu​​zz项目。 给定一系列数字 用Fizz替换为3的整数 用Buzz替换为5的整数 用FizzBu​​zz替换为3和5的整数。 这是一个非常简单的应用程序,但确实给我带来了一个问题。如何编写功能来测试多个requiremts,这些功能都是通过API上的一个方法调用触发的? 例如。 API调用如下
FizzBuzz.Replace(1,100);
使用Replace方法代码
public static string Replace (int min,int max)
{
       if (IsDiv3 && IsDiv5) {...}
       if (IsDiv3) {...}
       if (IsDiv5) {...}
       ...
}
我在SpecFlow中的功能如下:
Feature: FizzBuzz
    In order to display Fizz Buzz in       range of numbers
    As a user I want to be able to see Fizz Buzz replace certain numbers

Scenario: Replace muliples of three and five with Fizz,Buzz or FizzBuzz
    Given I have a range of numbers from 1 to 15
    When I press Submit
    Then the result should be
    | Numbers   |
    |   1   |
    |   2   |
    |   Fizz    |
    |   4   |
    |   Buzz    |
    |   Fizz    |
    |   7   |
    |   8       |
    |   Fizz    |
    |   Buzz    |
    |   11      |
    |   Fizz    |
    |   13      |
    |   14  |
    |   FizzBuzz|
一个问题是,如果我确实需要将所有要求集中到一个功能中,那么如何使该功能更有意义。 编辑 我正在努力创建多个方案,因为一旦创建第二个方案,第一个方案就会失败。
scenario 1: replace divisable by 3 with Fizz
Expected = 1 2 Fizz 4 5 Fizz 7 8 Fizz 10 11 Fizz 13 14 Fizz
Actual =   1 2 Fizz 4 5 Fizz 7 8 Fizz 10 11 Fizz 13 14 Fizz (First test)
Actual =   1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 Fizz (Second test)
然后做下一个场景
Scenario 2: replace divisable by 5 with Buzz
Expected = 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 Fizz
Actual =   1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 Fizz (Second test)
第二个方案通过了,但是第一个方案现在失败了。我不认为打开API来进行场景1、2、3对于该应用程序来说是一个很好的设计。 谢谢,     

解决方法

        在邮件列表中看到您的消息。如果您想对Fizz,Buzz和FizzBu​​zz进行测试,则可以更改规范以为实现的每个分支(即每个\“ if \”)创建一个方案:
Scenario: Replace multiples of three with Fizz
    Given I have a range of numbers from 1 to 4
    When I press Submit
    Then the result should be
    | Numbers    |
    |   1        |
    |   2        |
    |   Fizz     |
    |   4        |

Scenario: Replace multiples of five with Buzz
    Given I have a range of numbers from 4 to 5
    When I press Submit
    Then the result should be
    | Numbers    |
    |   4        |
    |   Buzz     |

Scenario: Replace multiples of three and five with FizzBuzz
    Given I have a range of numbers from 14 to 16
    When I press Submit
    Then the result should be
    | Numbers    |
    |   14       |
    |   FizzBuzz |
    |   16       |
您的示例中的另一个观察结果是,期望以表(基本上是数组)表示,但是结果以字符串表示。由于在此示例中您使用SpecFlow进行API测试,因此,我将尝试通过更改规范或您的API来使两者更加紧密地对齐。 例如,考虑以下API更改\“ public static string [] Replace(int min,int max)\”。然后,您的步骤将更容易编写-像这样:
[Binding]
public class FizzBuzzSteps
{
    private int _min;
    private int _max;
    private string[] _results;

    [Given(@\"I have a range of numbers from (\\d+) to (\\d+)\")]
    public void GivenIHaveARangeOfNumbers(int min,int max)
    {
        _min = min;
        _max = max;
    }

    [When(@\"I press Submit\")]
    public void WhenIPressSubmit()
    {
        _results = FizzBuzz.Replace(_min,_max);
    }

    [Then(@\"the result should be\")]
    public void ThenTheResultShouldBe(Table table)
    {
        for (var i = 0; i < table.RowCount; i++)
        {
            var expected = table.Rows[i][\"Numbers\"];
            var actual = _results[i];
            Assert.AreEqual(expected,actual);
        }
    }
}
HTH, 丹·莫克     ,        运行显示的规范时,specflow将建议您创建如下方法:
[Then(@\"the result should be\")]
    public void ThenTheResultShouldBe(Table table)
您可以像这样迭代并从表中检索值:
[Then(@\"the result should be\")]
    public void ThenTheResultShouldBe(Table table)
    {
        foreach (var row in table.Rows)
        {
            var numberValue = row[\"Numbers\"]; // the name of the column specified
        }
    }
至于如何使您的测试更有意义,我可以说的是,我个人会将测试分解为不同的场景。 我将为一种数字显示方案,将数字显示为其他数字除以3,另一种数字显示为5。