Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing login with devise and cucumber

I am trying to test the login functionality with Cucumber. My file users_steps.rb contains

Given /^I am a user named "([^"]*)" with an email "([^"]*)" and password "([^"]*)"$/ do |name, email, password|
  u = User.new(:name => name,
               :email => email,
               :password => password,
               :password_confirmation => password)
  u.skip_confirmation!
  u.save!
end


When /^I sign in as "(.*)\/(.*)"$/ do |email, password|
  #Given %{I am not logged in}
  When %{I go to the sign in page}
  And %{I fill in "user_email" with "#{email}"}
  And %{I fill in "user_password" with "#{password}"}
  And %{I press "Log Me In"}
end

Then /^I should be signed in$/ do
  Then %{I should see "Sign out"}
end

Then /^I should be signed in$/ do
  Then %{I should see "Sign out"}
end

Then /^I sign out$/ do
  visit('/account/logout')
end

and my cucumber scenario is:

  Scenario: User signs in successfully with email
    Given I am not logged in
    And I am a user named "foo" with an email "[email protected]" and password "please"
    When I go to the sign in page
    And I sign in as "[email protected]/please"
    Then I should be signed in
    When I return next time
    Then I should be already signed in

However this test fails to sign the user in. I have checked that the user is correctly created but after filling in the form I am redirected to the login page.

I am using capybara. What am I missing?

like image 306
bcouste Avatar asked Jun 24 '11 09:06

bcouste


1 Answers

You probably want to use :

1. In a user_steps.rb file located in step_definitions:

Given /^a valid user$/ do
  @user = User.create!({
             :email => "[email protected]",
             :password => "12345678",
             :password_confirmation => "12345678"
           })
end

Given /^a logged in user$/ do
  Given "a valid user"
  visit signin_url
  fill_in "Email", :with => "[email protected]"
  fill_in "Password", :with => "12345678"
  click_button "Sign in"
end

In your feature to test authentication:

Scenario: Login
  Given a valid user
  When I go to the login page
  And I fill in the following:
    |Email|[email protected]|
    |Password|12345678|
  And I press "Sign in"
  Then I should see "Signed in successfully."

Do not forget to change the path to your login page in the support/paths.rb

when /the login page/
  user_session_path

Here my path is using devise default setting. You can use rake routes to discover your login path.

You might have to change the text in the "Sign in", "Signed in successfully" to match your page. My assumptions here is that your are using the default config for cucumber+capybara+devise.

like image 130
vincent jacquel Avatar answered Oct 06 '22 00:10

vincent jacquel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!