Specflow/Gherkin:如何处理错误数据

3
我对 Specflow 还不熟悉,所以让我来尝试一下; 在我的下面示例中,我是否需要测试错误情况?如果我这样做,表格将不允许您添加错误值。因此,代码将出错,因为它作为坏类型输入。我想我只需要知道其他人如何处理验证用户无法输入错误数据?(由于涉及机密信息,字段名称已更改) 需求如下: 验证用户不能将费率设置为负数或大于1。费率范围从0.9999到0。 验证用户不能输入超过4个小数位的费率。
Given the lc does not exist  
    | Lc      |
    | Test90  |  
    | Test00  | 

    When I add a Lc  
    | Lc      | Somekind of ID|  Rate     |
    | Test90  | 2             |  0.9999   |
    | Test00  | 4             |  0        |

    Then the lc should be defined
    | Lc      | 
    | Test90  |
    | Test00  | 

我不确定表格不接受“错误值”的问题出在哪里。你是否遇到了某些异常?能否提供更多信息? - Sam Holder
1个回答

1

您的测试中测试了两个值,这有点奇怪。

我会倾向于像这样重新编写:

Scenario Outline: valid lc's can be created
    Given the lc named <lc> does not exist  
    When I add a Lc named <lc> with Id <id> and rate <rate>
    Then the lc named <lc> should be defined
Examples:
        | Lc      | Id |  Rate     |
        | Test90  | 2  |  0.9999   |
        | Test00  | 4  |  0        |


Scenario Outline: Invalid lc's cannot be created
    Given the lc named <Lc> does not exist  
    When I add a Lc named <Lc> with Id <Id> and rate <Rate>
    Then the lc named <Lc> should not be defined
    And an error should have been raised saying the rate was invalid
Examples:
        | Lc      | ID |  Rate     |
        | Test90  | 2  |  1.999    |
        | Test90  | 2  |  0.99999  |
        | Test90  | 2  |  -0.9     |
        | Test00Y | 4  |  6        |
        | Test00Y | 4  |           |

"And an error should have been raised saying the rate was invalid"这一步显然是可选的,但它表明由于无效汇率而预计会失败。"

谢谢Sam。当你说我测试2个值很奇怪时,应该如何做?我不需要测试所有有效的情况吗?就像在这种情况下,他们可以输入0或从0.0001到0.9999的值。但是他们不能留空或有负数或整数。 - Chris
奇怪的不是你在测试两个值,而是你通过表格而不是示例来测试两个值。我已经给出了使用具有示例的场景大纲的示例,而不是使用多个表格的示例。这使您可以轻松地为不同的值添加更多测试(例如负数和空值(我刚刚添加))。 - Sam Holder
我还纠正了使用场景大纲,这在示例中是必要的。 - Sam Holder
Gherkin 是 cucumber 和 specflow 理解的语言。 - Sam Holder
以上是我在Specflow中实现此功能的方式。 - Sam Holder
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接