Login for RSpec Request Spec


2012-07-25 · 1 min read

Integration tests based on RSpec request spec with Capybara are more elegant alternative to verbose and slow Cucumber tests. The only problem I've found so far is the absence of login helper, similar to sign_in method found in controller specs.

As Capybara is a library that simulates interactions with a browser, a possible solution would be a login form submission by posting authentication credentials. There is, however, more effective approach, i.e. setting session data directly through Warden.

Inside your RSpec request spec you have to include Warden's helpers to get access to login_as method as shown below:

include Warden::Test::Helpers

describe YourSpec
  let(:regular) { FactoryGirl.create(:user, :regular) }

  it 'signs in' do
    login_as regular, scope: :user
    visit profile_url
    expect {
      ...
    }.to()
    ...
  end
end