[rspec-users] Restful Authentication and Cucumber

Alberto Perdomo alberto.perdomo at aentos.es
Mon Dec 8 18:37:15 EST 2008


Hi Andrei,

>>> This is the step:
>>>
>>> Given /I am logged in as a user/ do
>>>  @current_user = Factory.define :user do |u|
>>>    u.name 'the user'
>>>    u.email 'erdoss at gmail.com'
>>>    u.login  'the_login'
>>>    u.password 'password'
>>>    u.password_confirmation 'password'
>>>  end
>>>  visits "/login"
>>>  fills_in("login", :with => "the_login")
>>>  fills_in("password", :with => "password")
>>>  clicks_button("Log in")
>>> end

you can simplify the code to activate the user.
This is my step definition for login:

 When /^i login$/ do
  user = Factory(:user)
  user.register!
  user.activate!
  fill_in("username", :with => user.login)
  fill_in("password", :with => user.password)
  click_button("Login")
end

This is my factory code in test/factories.rb

Factory.define :user do |u|
  u.login 'user'
  u.password 'password'
  u.password_confirmation 'password'
  u.email 'test at example.com'
end


I think i saw a fork of factory_girl somewhere that supported before
and after hooks so you could actually call the register and activate
methods after save from within the factory definition code. This way
your steps and other tests would be much cleaner. I will look further
into it.

Cheers,
Alberto.


More information about the rspec-users mailing list