小黄瓜多步定义匹配

时间:2019-10-16 12:27:09

标签: javascript bdd gherkin cucumberjs

我试图理解Gherkin,以至于我可以区分出用户故事并允许业务专家编写它们。

如果我有一个背景(第1版),这是一个常见的先决条件,为什么我的所有方案都出现错误,告诉我“多个步骤定义匹配”,这不是背景要点吗? >

如果我有不同的Given语句(第1版),那么基于不同的起始位置,“何时结束”是否不应该允许相同的动作?同样,“何时”会导致“多个步骤定义匹配”

这是我的功能文件。理想情况下,我想要的是版本1,这是业务专家如何编写它们的方法,它们是分开的,易于阅读的,但是要使它工作而没有“多个步骤定义匹配”错误的唯一方法是版本2,其中每个“当”需要组合时,更复杂,更难以阅读。

为什么我不能在超过1种情况下使用Background?

这些强迫改变的气味,所以我在做什么错,我错过了什么?我找到了数百个简单的示例,但我感到我缺少某种小黄瓜的指导原则。我想念什么?

Feature: Help
    When users says help
    "As a user
    I want the bot to understand me when I ask for help, 
    In order that I get some guidance, or some idea what to do next"

  # Version 1
    Background: 
      Given the user is in conversation with the bot
      *// above line causes error*

    Scenario: No topic
        Given there is no topic
         When the user says help
         *// above line causes error*
         Then the bot responds with a sorry, regretful message
          And then asks if the user would like to see a list of available features

    Scenario: A valid topic
        Given there is a valid topic
         When the user says help
         *// above line causes error*
         Then the bot responds with a confirmation message
          And then asks if the user would like to see a list of topic features

  # Version 2
    # Scenario: All
    #     Given the user is in conversation with the bot
    #      When the user says help and there is no topic
    #      Then the bot responds with a sorry, regretful message
    #       And then asks if the user would like to see a list of available features
    #      When the user says help and there is a valid topic
    #      Then the bot responds with a confirmation message
    #       And then asks if the user would like to see a list of topic features


Failures:

1) Scenario: No topic # features/help.feature:10
   ✖ Given the user is in conversation with the bot
       Multiple step definitions match:
         the user is in conversation with the bot - tests/feature_definitions/help_definition.js:4 
         the user is in conversation with the bot - tests/feature_definitions/help_definition.js:21
   - Given there is no topic # tests/feature_definitions/help_definition.js:7
   ✖ When the user says help
       Multiple step definitions match:
         the user says help - tests/feature_definitions/help_definition.js:10
         the user says help - tests/feature_definitions/help_definition.js:27
   - Then the bot responds with a sorry, regretful message # tests/feature_definitions/help_definition.js:13
   - And then asks if the user would like to see a list of available features # tests/feature_definitions/help_definition.js:16

2) Scenario: A valid topic # features/help.feature:16
   ✖ Given the user is in conversation with the bot
       Multiple step definitions match:
         the user is in conversation with the bot - tests/feature_definitions/help_definition.js:4 
         the user is in conversation with the bot - tests/feature_definitions/help_definition.js:21
   - Given there is a valid topic # tests/feature_definitions/help_definition.js:24
   ✖ When the user says help
       Multiple step definitions match:
         the user says help - tests/feature_definitions/help_definition.js:10
         the user says help - tests/feature_definitions/help_definition.js:27
   - Then the bot responds with a confirmation message # tests/feature_definitions/help_definition.js:30
   - And then asks if the user would like to see a list of topic features # tests/feature_definitions/help_definition.js:33

2 个答案:

答案 0 :(得分:0)

您的步骤有多个定义。黄瓜每步只允许一个步定义。根据您的描述,您似乎为每种情况都提供了一个步骤定义,因此这两种情况都有各自的When the user says help定义。这是定义步骤定义的错误方法。每个步骤应该只有一个定义,因此会出现错误。

When the user says help的定义应适用于正在使用此步骤的 所有 个方案。它的定义应该是通用的。

如果根据场景需要改变同一步骤,则有两种选择:

  1. 创建一个单独的测试项目,以便您可以拥有2个全局唯一的定义,因为对于步骤定义您将拥有2个单独的全局上下文。

  2. 参数化步骤

参数化步骤可能是最简单的事情,我将以When the user says help为例:

Scenario: No topic
    Given there is no topic
     When the user says "help"
     Then the bot responds with a sorry, regretful message
      And then asks if the user would like to see a list of available features

上面的行将单词help放在双引号中。生成步骤定义将使您可以将引号之间的文本作为参数传递给您的步骤定义方法:

When(/the user says "([^"]+)"/, function(textToSay) {
    // "speak" to the chat bot using whatever API you have defined
    chatBot.receiveMessage(textToSay);
});

现在,这一步可以接受多段文字:

When the user says "help"
...

When the user says "list topics"
...

When the user says "I WANT TO SPEAK TO A REAL PERSON!!!!"
Then the bot responds with "I'm sorry, Dave. I'm afraid I can't do that."

(好吧,我迈出了最后一步,但是如果机器人不明白用户在说什么,那将是一个有趣的回答)

答案 1 :(得分:0)

非常感谢您的回答。我也去过黄瓜松弛的支持渠道,在所有这些想法之中,一分钱都掉了。

我阅读的每个示例都显示了功能文件与步骤定义文件一对一映射。

在我看来,这些功能在终端窗口中创建了模板,然后将其放入step.js文件中,这些文件比在测试中运行的文件要好,毕竟是.js!终端模板是重复的,我将它们的功能场景复制到了steps.js 1对1中

我并没有想到功能文件仍然是过程的一部分,并且实际上是测试运行时的步骤驱动程序。

我现在将自己的步骤组织在一个完全不同的文件夹文件结构中,不再与功能文件按1对1的顺序进行交互,这是我所见过的任何示例,用户所说的,机器人的响应,主题等等...,这一切现在变得更加有意义,当然,它删除了所有重复的步骤。

再次感谢向正确方向推动思想的每个人:-)