Nov 10 2011

How to test devise features with Cucumber

Cucumber Ruby | comments

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, :username=>username, :first_name=>first_name, :last_name=> last_name, :email => email, :password => password, :password_confirmation => password)
                @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, :username=>username, :first_name=>first_name, :last_name=> last_name, :email => email, :password => password, :password_confirmation => password)
                @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 = "testing@testemail.net"
                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 = "admin@testemail.net"
                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
                email = "testing@testemail.net"
                password = "secretpass"
                first_name = "Katherine"
                last_name = "Pe"
                username = 'bridgeutopia'
              
                member_role = Factory(:member_role)  
                @user = Factory(:user, :username=>username, :first_name=>first_name, :last_name=> last_name, :email => email, :password => password, :password_confirmation => password)
                @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 = "admin@testemail.net"
                password = "secretpass"
                first_name = "Katherine"
                last_name = "Pe"
                username = "katherine_pe"
              
                admin_role = Factory(:admin_role)
                @admin = Factory(:user, :username=>username, :first_name=>first_name, :last_name=> last_name, :email => email, :password => password, :password_confirmation => password)
                @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.