【问题标题】:Array placeholder in Gherkin syntaxGherkin 语法中的数组占位符
【发布时间】:2015-03-12 15:42:18
【问题描述】:

您好,我正在尝试用 gherkin 语法来表达一组需求,但它需要大量重复。我看到here 我可以使用非常适合我的任务的占位符,但是我的 Given 和 then 中的一些数据是集合。我将如何在示例中表示集合?

Given a collection of spaces <spaces>
    And a <request> to allocate space
When I allocate the request
Then I should have <allocated_spaces>

Examples:
| spaces | request | allocated_spaces |
|  ?     |  ?      |  ?               |

【问题讨论】:

    标签: cucumber bdd gherkin


    【解决方案1】:

    有点hacky,但你可以分隔一个字符串:

    Given a collection of spaces <spaces>
        And a <request> to allocate space
    When I allocate the request
    Then I should have <allocated_spaces>
    
    Examples:
    | spaces    | request | allocated_spaces |
    |   a,b,c   |  ?      |  ?               |
    
    Given(/^a collection of spaces (.*?)$/) do |arg1|
      collection = arg1.split(",")  #=> ["a","b","c"]
    end
    

    【讨论】:

      【解决方案2】:

      您可以使用Data Tables。我以前从未尝试在数据表中使用参数,但理论上它应该可以工作。

      Given a collection of spaces:
      | space1        |
      | space2        |
      | <space_param> |
      And a <request> to allocate space
      When I allocate the request
      Then I should have <allocated_spaces>
      
      Examples:
      | space_param | request | allocated_spaces |
      |  ?          |  ?      |  ?               |
      

      给定的数据表将是Cucumber::Ast::Table 的一个实例,请查看 ruby​​doc 的 API。

      【讨论】:

      • 我现在已经对其进行了测试,它确实有效。这应该是正确的答案。并且比用户使用列更好。
      【解决方案3】:

      这是一个示例,再次使用拆分,但没有正则表达式:

        Scenario Outline: To ensure proper allocation
          Given a collection of spaces <spaces>
            And a <request> to allocate space
           When I allocate the request
           Then I should have <allocated_spaces>
      
          Examples:
            | spaces       | request | allocated_spaces |
            | "s1, s2, s3" | 2       | 2                |
            | "s1, s2, s3" | 3       | 3                |
            | "s1, s2"     | 3       | 2                |
      

      我使用 cucumber-js,所以代码可能是这样的:

      Given('a collection of spaces {stringInDoubleQuotes}', function (spaces, callback) {
          // Write code here that turns the phrase above into concrete actions
          this.availableSpaces = spaces.split(", ");
          callback();
      });
      
      Given('a {int} to allocate space', function (numToAllocate, callback) {
          this.numToAllocate = numToAllocate;
          // Write code here that turns the phrase above into concrete actions
          callback();
      });
      
      When('I allocate the request', function (callback) {
          console.log("availableSpaces:", this.availableSpaces);
          console.log("numToAllocate:", this.numToAllocate);
          // Write code here that turns the phrase above into concrete actions
          callback();
      });
      
      Then('I should have {int}', function (int, callback) {
          // Write code here that turns the phrase above into concrete actions
          callback(null, 'pending');
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-03
        • 2013-09-13
        • 1970-01-01
        相关资源
        最近更新 更多