Devise/Cucumber - Adding a step which confirm a user exists
Asked Answered
P

3

7

I'm new to cucumber and I find following snippets to test the Devise login feature. However it seems one more step missing, and I didn't find any solution:

Given /^that a confirmed user exists$/ do
  pending # express the regexp above with the code you wish you had
end

Here the following code:

features/authentication/session.feature

Feature: Session handling
  In order to use the site
  As a registered user
  I need to be able to login and logout

Background: 
  Given that a confirmed user exists

Scenario Outline: Logging in
  Given I am on the login page
  When I fill in "user_email" with "<email>"
  And I fill in "user_password" with "<password>"
  And I press "Sign in"
  Then I should <action>
  Examples:
    |         email       |  password   |              action             |
    | [email protected] |  test1234   | see "Signed in successfully"    |
    | [email protected]     |  password   | see "Invalid email or password" |

Scenario: Logging out
  Given I am logged in
  When I go to the sign out link
  Then I should see "Signed out successfully"

features/step_definitions/authentication_steps.rb

# Session
Given /^I am logged in$/ do
  visit path_to('the login page')
  fill_in('user_email', :with => @user.email)
  fill_in('user_password', :with => @user.password)
  click_button('Sign in')
  if defined?(Spec::Rails::Matchers)
    page.should have_content('Signed in successfully')
  else
    assert page.has_content?('Signed in successfully')
  end
end

spec/factories/user.rb

Factory.define :minimal_user, :class => User do |u|
  u.username 'minimal'
  u.email '[email protected]'
  u.password 'test1234'
  u.password_confirmation 'test1234'
end

Here the link to the orginal code

Many thanks for your help!!

Progenitive answered 2/11, 2010 at 23:31 Comment(0)
A
3

Your title says "validate that a user exists", but that's might not be what you need to do there -- your Given steps don't need to be asserting that something worked, they invoke code to create the application state for your scenario. Of course, they're still tests since they can still fail.

I think you're looking for something like this:

Given /^that a confirmed user exists$/ do
  Factory.create(:minimal_user)
end

That will create and save a new confirmed user from your factory definition so the rest of the Scenario can proceed.

Attis answered 2/11, 2010 at 23:38 Comment(4)
No problem! I just edited to add some more information RE: given steps.Attis
OK Daniel, So I'll change the title. I have also 2 errors resulting of that code. I would be very pleased if you can give me a hand on this. I add the failed steps as an answer. Thank you!Progenitive
It looks like you have a couple structural issues, probably what's happening is your Scenario Outline, which runs before every Scenario in the feature, is signing in and then when you run your Given I am logged in step, it doesn't find the message since you're already logged in (I think the Devise login page forwards active users to the root by default). The other issue is that it looks like @user isn't being defined anywhere. You might want to read the excellent official cucumber docs: github.com/aslakhellesoy/cucumber/wikiAttis
Thanks again Daniel for your time! BenoitProgenitive
P
1

To complete the Daniel's answer and because I enable the Devise confirmable module, I should add a line in my fixture in order to tell that the user is confirmed.

For example:

Factory.define :minimal_user, :class => User do |u|
  u.username 'minimal'
  u.email '[email protected]'
  u.password 'test1234'
  u.password_confirmation 'test1234'
  u.confirmed_at 'here the date you want'
end

Also, debug steps you can find here are very useful.

Hope it helps some people.

Progenitive answered 3/11, 2010 at 14:19 Comment(1)
In the future, you shouldn't add this as an answer. Instead you should update your question.Kwapong
P
0
(::) failed steps (::)

expected #has_content?("Signed in successfully") to return true, got false (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/web_steps.rb:110:in `block (2 levels) in <top (required)>'
./features/step_definitions/web_steps.rb:14:in `with_scope'
./features/step_definitions/web_steps.rb:108:in `/^(?:|I )should see "([^"]*)"(?: within "([^"]*)")?$/'
features/authentication/session.feature:16:in `Then I should <action>'

undefined method `email' for nil:NilClass (NoMethodError)
./features/step_definitions/authentication_steps.rb:43:in `/^I am logged in$/'
features/authentication/session.feature:23:in `Given I am logged in'
Progenitive answered 3/11, 2010 at 0:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.