How to test devise features with Cucumber

Updated the Devise wiki page on “How To: Test with Cucumber” on github. Following the guide should work.

I recently got this warning:

WARNING: i18n methods within step definitions are deprecated. Use #step instead

Was wondering what was wrong only to find out I had to rewrite code for testing some devise features.

Here’s what I had before:

Given /^I have one user "([^"]*)" "([^"]*)" with password "([^"]*)" and my first name is "([^"]*)" and last name is "([^"]*)"$/ do |username, email, password,  first_name, last_name|
  member_role = Factory(:member_role)  
  @user = Factory(:user)
  @user.roles <<  member_role
  @user.confirm! 
end

Given /^I am an admin and have one user "([^"]*)" "([^"]*)" with password "([^"]*)" and my first name is "([^"]*)" and last name is "([^"]*)"$/ do |username, email, password,  first_name, last_name|
  admin_role = Factory(:admin_role)
  @admin = Factory(:user)
  @admin = User.where(:email=>email).first
  @admin.roles <<  admin_role
  @admin.confirm!
end


When /^the following (.+) records exist?$/ do |factory, table|
  table.hashes.each do |hash|
    Factory(factory, hash)
  end
end


Given /^I am a registered user$/ do
  email = "[email protected]"
  password = "secretpass"
  first_name = "Katherine"
  last_name = "Pe"
  username = 'bridgeutopia'

  Given %{I have one user "#{username}" "#{email}" with password "#{password}" and my first name is "#{first_name}" and last name is "#{last_name}"}
  And %{I go to the login page}
  And %{I fill in "user_email" with "#{email}"}
  And %{I fill in "user_password" with "#{password}"}
  And %{I press "Submit"}


end

Given /^I am an admin user$/ do
  email = "[email protected]"
  password = "secretpass"
  first_name = "Katherine"
  last_name = "Pe"
  username = "katherine_pe"

  Given %{I am an admin and have one user "#{username}" "#{email}" with password "#{password}" and my first name is "#{first_name}" and last name is "#{last_name}"}
  And %{I go to the login page}
  And %{I fill in "user_email" with "#{email}"}
  And %{I fill in "user_password" with "#{password}"}
  And %{I press "Submit"}
end

So apparently that has to change to something like this:

Given /^I am a registered user$/ do
  member_role = Factory(:member_role)  
  @user = Factory(:user) 
  @user.roles <<  member_role
  @user.confirm!

  visit '/login'
  fill_in "user_email", with: email
  fill_in "user_password", with: password
  click_button "Submit"
  
end

Given /^I am an admin user$/ do
  email = "[email protected]"
  password = "secretpass"
  first_name = "Katherine"
  last_name = "Pe"
  username = "katherine_pe"

  admin_role = Factory(:admin_role)
  @admin = Factory(:user) 
  @admin = User.where(email: email).first
  @admin.roles <<  admin_role
  @admin.confirm!
  
    
  visit '/login'
  fill_in "user_email", with: email
  fill_in "user_password", with: password
  click_button "Submit"
    
end

And that’s a lot cleaner.