如何允许多个步骤定义匹配

How to allow multiple step definitions match

我正在使用 Cucumber 进行 BDD 测试,功能文件如下所示:

Feature: Sing Up

  Scenario: User tries to sign up without interests 
    Given the interests are empty
    When user presses on sign up
    Then it should show "The interests field is empty."

  Scenario: User tries to sign up with 2 interests 
    Given 2 interests are entered
    When user presses on sign up
    Then it should show "The interests should be at least three."

我在 JS 中创建了这些步骤:

const assert = require("assert");
const { Given, When, Then } = require("cucumber");

Given("the interests are empty", function() {
  // Write code here that turns the phrase above into concrete actions
  return "pending";
});

When("user presses on sign up", function() {
  // Write code here that turns the phrase above into concrete actions
  return "pending";
});

Then("it should show {string}", function(string) {
  // Write code here that turns the phrase above into concrete actions
  return "pending";
});

/*----------------------------------------------------------------------*/

Given("{int} interests are entered", function(int) {
  // Given('{float} interests are entered', function (float) {
  // Write code here that turns the phrase above into concrete actions
  return "pending";
});

When("user presses on sign up", function() {
  // Write code here that turns the phrase above into concrete actions
  return "pending";
});

Then("it should show {string}", function(string) {
  // Write code here that turns the phrase above into concrete actions
  return "pending";
});

编译器抱怨:

   ✖ When user presses on sign up
       Multiple step definitions match:
         user presses on sign up - features/step_definitions/signup/steps.js:9 
         user presses on sign up - features/step_definitions/signup/steps.js:27

如何在不同的scenario中允许相同的when

您的步骤定义必须是唯一的,您不必为每个场景重新定义它们。编译器告诉您 user presses on sign up 步骤已定义两次(与 it should show {string} 相同)。