1

2 つの異なるシナリオでほぼ同様の手順を実行します。一歩だけ違います。ステップを再利用するための好ましい方法はありますか。

    @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」を選択し、残りは同じです。別のシナリオで手順を再開する方法。

これがステップの定義です。ここでの同じ問題は、1 行を除いて 2 つの異なるステップで同じコードを使用しています。

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
4

2 に答える 2

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 回実行されます。

それが DRY シナリオへの簡単な方法です

于 2013-05-29T19:21:31.460 に答える
2

Cucumber 側 : シナリオ アウトラインを使用する必要があります

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
于 2013-05-29T18:43:25.863 に答える