2013-05-29 63 views
1

對於兩種不同的情況,我幾乎都有類似的步驟。只有一個步驟是不同的。有沒有任何優選的方式來重複使用步驟。如何在黃瓜測試中重複使用步驟

@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 | 
     | [email protected] | password | 

    Then I should see "[email protected]" 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 "[email protected]" 
    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 | 
     | [email protected] | password | 

    Then I should see "[email protected]" 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 "[email protected]" 
    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("[email protected]") 
    user = User.find_by_email("[email protected]") 
    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("[email protected]") 
    user = User.find_by_email("[email protected]") 
    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

黃瓜的一面:你應該使用一個場景大綱

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 | 

,然後進行乾燥,我會改變你的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("[email protected]") 
    ... 
    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 
3

您應該使用background重用所有常見在feature內出現在scenarios中的代碼。舉個簡單的例子,你有

Then I should see "[email protected]" 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 "[email protected]" 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將每一次方案之前運行。

這就是乾爽場景的簡單方法

+0

太好了。 :)我怎麼能做到這一點user.abc_id.should == 0?我geting錯誤說不能比較零。 – user588324

+0

沒問題。剛開始另一個問題,我會盡力幫助你。更多的人會看到它,你會得到更好的幫助,它不會改變當前的問題。 – fontno