I find that writing features can get really repetitive. So to get rid of writing steps over and over again for multiple models I decided to create a pretty simple solution. It's not big enough to yield a gem so it's here for your use / modification. Comments or questions are welcome, would love to add to this as well.
step 'I modify the :model :attribute with/to :value' do |model_name, attribute, value|
id = "#{model_name}_#{attribute}"
fill_in "#{id}", with: value
form_modifications[id.to_sym] = value
end
step 'I modify/change the :model :attribute' do |model_name, attribute|
value = "A Value"
id = "#{model_name}_#{attribute}"
fill_in "#{id}", with: value
form_modifications[id.to_sym] = value
end
step "I submit the form" do
find(:xpath, '//input[@type="submit"]').click
end
step "I click the :link_name link" do |link_name|
click_link link_name
end
step "I visit the :path page" do |path|
route_method = "#{path.underscore}_path"
visit routes.send(route_method)
end
step "the :model should be updated" do |model|
find('div.alert').text.should match /Updated/i
end
step "the :model should be created" do |model|
find('div.alert').text.should match /Created/i
end
Create a support file called step_helper.rb
and include the following:
module StepHelper
def routes
Rails.application.routes.url_helpers
end
def form_modifications
@form_modifications ||= {}
end
end
Then in your spec_helper include this:
RSpec.configure do |config|
config.include StepHelper
end
Done!
Now you can write features like this:
Feature: A user can register
Scenario: I create a new account
When I visit the register page
Then I modify the user name with "Robert Ross"
Then I modify the user email with "robert@creativequeries.com"
Then I modify the user password with "password"
Then I modify the user password_confirmation with "password"
Then I modify the user organization_name with "Test Client"
When I submit the form
Then I've created an account
My idea for form modifications is you can easily check that a model was updated or created. I have this in a user steps file:
step "I've created an account" do
User.find_by_email(form_modifications[:user_email]).should_not be_nil
end