Gherkin语法中的数组占位符

时间:2015-03-12 15:42:18

标签: cucumber bdd gherkin

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

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 |
|  ?     |  ?      |  ?               |

3 个答案:

答案 0 :(得分:3)

有点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

答案 1 :(得分:0)

您可以使用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的实例,请检查rubydoc的API。

答案 2 :(得分:0)

这是一个例子,再次使用split,但没有正则表达式:

  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');
});
相关问题