Reputation: 310
I almost have similar steps for two different scenario. Only one step is different. Is there any preferred way to reuse steps.
@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
Only step is changing here is
And I choose "abc_id_yes" and rest are same. How I can resue the steps in different scenario.
Here are the steps definition. Same issue here I am using same code in two different steps except one line.
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
Upvotes: 1
Views: 1149
Reputation: 6852
You should use background
to reuse all common code that appears in scenarios
within the feature
. For a quick example, you have
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.......
in both scenarios. Now you can but these in a 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
Now the background
will be run once before every scenario.
Thats the easy way to DRY scenarios
Upvotes: 3
Reputation: 760
Cucumber side : you should use a Scenario Outline
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 |
And then, for DRY, I would change your 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
Upvotes: 2