Ruby on rails 如何在测试中重用步骤

Ruby on rails 如何在测试中重用步骤,ruby-on-rails,cucumber,Ruby On Rails,Cucumber,对于两种不同的场景,我几乎有相似的步骤。只有一步是不同的。是否有重用步骤的首选方法 @no-database-cleaner Feature: Managing users parent child relationships In order to use Login portal I want to create parent child relationships Scenario: Creating a child user with new a

对于两种不同的场景,我几乎有相似的步骤。只有一步是不同的。是否有重用步骤的首选方法

    @no-database-cleaner
    Feature: Managing users parent child relationships
    In order to use Login portal
    I want to create parent child relationships


  Scenario:  Creating a child user with new ar_id
    Given I am on the homepage

    When I attempt to sign in with following user account:
      | email address         | password |
      | abc@company1.com   | password |

    Then I should see "abc@company1.com" message on page
    When I follow "All Child Users"
    Then I should see "Add Sub Child"
    When I click "Add Sub Child"
    Then I should see "Child Sub User"
    And I fill in "Email" with "xyztest@gmail.com"
    And I select "Medium" from "filter_level"
    And I choose "abc_id_yes"
    When I press "Create Child User"
    Then I should see "Child User is successfully created."
    And appropriate records should get created for the child user for new abc_id


  Scenario:  Creating a child user with abc_id with value zero
    Given I am on the homepage

    When I attempt to sign in with following user account:
      | email address         | password |
      | recdns@company1.com   | password |

    Then I should see "recdns@company1.com" message on page
    When I follow "All Child Users"
    Then I should see "Add Sub Child"
    When I click "Add Sub Child"
    Then I should see "Child Sub User"
    And I fill in "Email" with "xyztest1@gmail.com"
    And I select "Medium" from "filter_level"
    And I choose "abc_id_no"
    When I press "Create Child User"
    Then I should see "Child User is successfully created."
    And appropriate records should get created for the child user for default abc_id
这里唯一的改变就是

我选择abc\u id\u yes,其余的都一样。如何在不同场景中继续执行这些步骤

以下是定义的步骤。同样的问题在这里,除了一行代码外,我在两个不同的步骤中使用相同的代码

Then(/^appropriate records should get created for the child user for new abc_id$/) do
  parent_user = User.find_by_email("abc@company1.com")
  user = User.find_by_email("xyztest@gmail.com")
  user.default_filter_level.should be_true
  user.abc_id.should be_true
  user.parent_id.should == parent_user.id
  filter = Filter.find_by_user_id(user.id)
  filter.user_id.should == user.id
  filter.abc_id.should be_true
  filter.account_id.should == user.account.id
end

Then(/^appropriate records should get created for the child user for default abc_id$/) do
  parent_user = User.find_by_email("recdns@company1.com")
  user = User.find_by_email("xyztest1@gmail.com")
  user.default_filter_level.should be_true
  user.abc_id.should == 0 ##this is different
  user.parent_id.should == parent_user.id
  filter = Filter.find_by_user_id(user.id)
  filter.user_id.should == user.id
  filter.abc_id.should == 0 ##this is different
  filter.account_id.should == user.account.id
end

黄瓜面:你应该使用场景大纲

Scenario Outline: Creating a child user with new ar_id
  Given I am on the homepage
  ...
  Then I should see "Child User is successfully created."
  And appropriate records should get created for the child user for <my_id>

  Scenarios:
  | my_id |
  | default abc_id |
  [ new abc_id |

黄瓜面:你应该使用场景大纲

Scenario Outline: Creating a child user with new ar_id
  Given I am on the homepage
  ...
  Then I should see "Child User is successfully created."
  And appropriate records should get created for the child user for <my_id>

  Scenarios:
  | my_id |
  | default abc_id |
  [ new abc_id |
您应该使用background来重用功能中场景中出现的所有公共代码。举个简单的例子,你有

Then I should see "abc@company1.com" message on page
When I follow "All Child Users"
Then I should see "Add Sub Child"
When I click "Add Sub Child"
etc.......
在这两种情况下。现在,您可以在后台查看这些内容

现在,在每个场景之前,后台将运行一次

这是干燥场景的简单方法

您应该使用后台来重用功能中场景中出现的所有通用代码。举个简单的例子,你有

Then I should see "abc@company1.com" message on page
When I follow "All Child Users"
Then I should see "Add Sub Child"
When I click "Add Sub Child"
etc.......
在这两种情况下。现在,您可以在后台查看这些内容

现在,在每个场景之前,后台将运行一次


这是干燥场景的简单方法

太好了:我如何处理这个user.abc_id.should==0?我得到的错误是不能比较零。没问题。再问一个问题,我会尽力帮你的。更多的人会看到它,你会得到更好的帮助,它不会改变当前的问题。太好了我如何处理这个user.abc_id.should==0?我得到的错误是不能比较零。没问题。再问一个问题,我会尽力帮你的。更多的人会看到它,你会得到更好的帮助,它不会改变当前的问题。