7

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 "user@test.com" and password "please"
    When I go to the sign in page
    And I sign in as "user@test.com/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?

bcouste
  • 121
  • 1
  • 6

1 Answers1

2

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 => "minikermit@hotmail.com",
             :password => "12345678",
             :password_confirmation => "12345678"
           })
end

Given /^a logged in user$/ do
  Given "a valid user"
  visit signin_url
  fill_in "Email", :with => "minikermit@hotmail.com"
  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|minikermit@hotmail.com|
    |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.

nowk
  • 32,822
  • 2
  • 35
  • 40
vincent jacquel
  • 5,107
  • 2
  • 17
  • 18
  • Thank you for your reply. Sadly I was unable to fix so I refactored my code and posted [another question](http://stackoverflow.com/questions/6505498/cucmber-bdd-with-capybara-and-devise-integration) – bcouste Jun 28 '11 at 11:14
  • wouldn't it be DRYer to fill in the login information from the user instance variable? E.g. fill_in "Email", :with => @user.email – KobeJohn Jan 23 '12 at 17:09
  • Can't I sign in with my own user? https://stackoverflow.com/questions/21417708/cucumber-signing-in-isnt-working – quantumpotato Jan 28 '14 at 21:54