黄瓜步骤定义是全球性的吗?

时间:2014-10-03 14:16:39

标签: cucumber cucumberjs

我正在学习Cucumber,并注意到如果两个完全独立的功能有两个意外措辞相同的步骤,Cucumber建议他们只有一个步骤定义。这是否意味着步骤定义是全局的并且它们是要共享的?

实施例

假设一个业务分析师团队正在为一家拥有银行部门和经纪部门的金融公司编写规范。进一步假设两个不同的人正在为各自的部门编写功能来计算交易费用。

银行家伙写道:

Feature: Transaction Fees
    Scenario: Cutomer withdraws cash from an out-of-netwrok ATM
        Given that a customer has withdrawn cash from an out-of-netwrok ATM
        When I calculate the transaction fees
        Then I must include an out-of-netwrok ATM charge
经纪人写道

Feature: Transaction Fees
    Scenario: Cutomer places a limit order
        Given that a customer has placed a limit order
        When I calculate the transaction fees
        Then I must include our standard limit-order charge

请注意,两个方案的When子句相同。更糟糕的是,两个人都将这个场景放在一个名为transaction-fees.feature的文件中(当然在不同的目录中)。

Cucumber为步骤定义提出以下建议:

You can implement step definitions for undefined steps with these snippets:

this.Given(/^that a customer has withdrawn cash from an out\-of\-netwrok ATM$/, function (callback) {
  // Write code here that turns the phrase above into concrete actions
  callback.pending();
});

this.When(/^I calculate the transaction fees$/, function (callback) {
  // Write code here that turns the phrase above into concrete actions
  callback.pending();
});

this.Then(/^I must include an out\-of\-netwrok ATM charge$/, function (callback) {
  // Write code here that turns the phrase above into concrete actions
  callback.pending();
});

this.Given(/^that a customer has placed a limit order$/, function (callback) {
  // Write code here that turns the phrase above into concrete actions
  callback.pending();
});

this.Then(/^I must include our standard limit\-order charge$/, function (callback) {
  // Write code here that turns the phrase above into concrete actions
  callback.pending();
});

请注意,when子句仅建议一次。

  1. 这是否意味着只需要只有一个步骤定义需要在两个步骤定义文件中的一个中输入?​​
  2. 黄瓜是否将功能文件与名称相同的step_definition文件相关联?换句话说,它是否将transaction-fees.feature与transaction-fees.steps.js相关联?如果所有步骤定义都是全局的,那么我可能会错误地认为文件/目录设置仅适用于组织,并且就执行环境而言并不意味着什么。
  3. 提前感谢您的时间和澄清。

1 个答案:

答案 0 :(得分:2)

步骤defs附加到World对象,这是"这个"在上面的代码中。

  1. 应该只有一个步骤定义。它们是为了共享。 IIRC,Cucumber Boo,第149页(https://pragprog.com/book/hwcuc/the-cucumber-book)详细介绍了这一设计决定。虽然它是红宝石,但我认为这在所有黄瓜实施中都是一样的。

  2. Cucumber不会关联要素文件和step_definition文件。文件树/约定仅为方便起见。