Non Rails/Ruby people may as well ignore this one!
Sometimes you need to see what the web server is sending back, when I’m working on rspec scripts I can print things out (which messes up the pretty output) but then see where my assumptions are wrong. I needed to be able to see what was in the response so that I could work out what was wrong. It’s annoyingly easy when you work out how. You need to add a step that looks like this:

When /^I dumped the response$/ do
  puts response.body
  # capybara users puts body
end
Then, in the features that are giving trouble:
Scenario: creator restores fragment
  When fragment 1 exists
  And I am the fragment owner
  And I am on the fragment
  And I dumped the response
  # ...

This allows you to do some debugging on that error you can’t fathom out. It helped me greatly when I put in devise authentication and discovered that my code was looping back to the login screen because of a duff password.

Devise

I love this plugin – takes away a ton of work (Thanks Colin)

This should get you started with cucumber. Note that password is a helper method that just returns something like ‘test123’. My code has user roles (I do it differently from the way the devise guys do it) and I took it out, so it may not run straight away as it’s untested. Caveat emptor.

Bootnote Suggest you look at the devise test helpers before you do anything here.

def create_my_user(params)
  unless user = User.find_by_email(params[:email])
    params[:password_confirmation] = params[:password]
    user = User.create!(params)
    ## This makes the user look 'confirmed'
    user.update_attribute(:confirmation_token,nil)
    user.update_attribute(:confirmed_at,Time.now)
  end
  user
end

Given /^I am logged in as (.*)$/ do |email|
@current_user = create_my_user(:email => email, :password => password )
visit new_user_session_path
fill_in(“Email”, :with => email )
fill_in(“Password”, :with => password )
click_button(“Sign in”)
response.body.should =~ /My Lovely App/m
end

How I now do this in RSpec

  let(:customer) { Factory(:customer) }
 before :each do
    sign_in customer
  end

Also needs this in your spec helper file:

 RSpec.configure do |config|
    config.include Devise::TestHelpers, :type => :controller
  end

Imported Comments:

Kyle

Found this useful 🙂

James Ferguson

You can just do:
user.confirm!

Also you can put steps within your steps, since they’re generally clearer to read:

Given /^I am logged in as (.*)$/ do |email|
@current_user = create_my_user(:email => email, :password => password )
steps %Q{
Given I am on the new user session page
And I fill in “Email” with “#{email}”
And I fill in “Password” with “#{password}”
And I press “Sign in”
Then I should see “My Lovely App”
}
end