breaking into the unknown…

devise user login for rspec controllers


Recently, I get into problem while running controller spec. Iam using rspec with capybara and using devise gem for user authentication. While testing controller, I need to login a user. I tried to use Devise built in helper method sign_in and sign_out to manage user login logout, but it is not working at all. After, a lot of googling I find the solution at capybara wiki.

Devise’s built in sign_in and sign_out methods is not working  because these require direct access to the request object which is not available while using Capybara. If, you are aware, Devise itself build over warden interface and warden do provide test helper to manage user login logout. So by,  including warden test helper in controller spec, we can manage user login logout. The method provided by warden is:  login_as and logout.

The sample code from one of my controller spec is as below

require ‘spec_helper’
describe LoanDocumentsController do

     before(:each) do
           @user = FactoryGirl.create(:user)
           login_as @user
           @user.terms_accepted_at =
          @user.terms_declined_at = nil

  after(:each) do
  describe “GET ‘index'” do
    include Warden::Test::Helpers  # this will include warden test helper and make login_as and logout method available to you
    Warden.test_mode!                      # setting warden API to test mode

    it “should load the index page if user logged in” do
      visit loan_documents_path
      page.should have_content(“Document Status”)



Since , we have authentication in all of our controller, instead of specifying warden configuration in all the controller spec, it is better to specify it once in spec_helper.rb file. As we can see, we are already requiring the spec_helper.rb. the configuration written in this file will be available to us. The spec_helper.rb with warden configuration will look like this

RSpec.configure do |config|

   …othe configuration..

  after(:each) do


Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

3 thoughts on “devise user login for rspec controllers

  1. Reblogged this on My Corner of the Web and commented:
    Speed up Rspec testing!!! Thank you thank you.

  2. Thanks!

    after(:each) do

    should be:

    config.after(:each) do

  3. Hey there! Someone in my Facebook group shared this website with
    us so I came to check it out. I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers!
    Excellent blog and great design and style.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s