如何在黄瓜测试中重复使用步骤

时间:2013-05-29 17:57:14

标签: 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 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_id_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

2 个答案:

答案 0 :(得分:3)

您应该使用background重用scenariosfeature中显示的所有常用代码。有一个简单的例子,你有

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

在两种情况下。现在你可以在background

中进行这些操作
Feature: Managing users parent child relationships
  In order to use Login portal
  I want to create parent child relationships

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

  Scenario: # first scenario
    # this is different

  Scenario: # second scenario
    # this is different

现在background将在每个场景之前运行一次。

这是干燥方案的简便方法

答案 1 :(得分:2)

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

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 |

然后,对于DRY,我会更改你的step_definition:

Then(/^appropriate records should get created for the child user for (default|new) abc_id$/) do |which_id|
  parent_user = User.find_by_email("recdns@company1.com")
  ...
  if (which_id == "new")
    user.abc_id.should be_true
  else # default
    user.abc_id.should == 0 ##this is different
  end
  ..
  if (which_id == "new")
    filter.abc_id.should be_true
  else # default
    filter.abc_id.should == 0 ##this is different
  end
  filter.account_id.should == user.account.id
end
相关问题