Search code examples
ruby-on-railsrubyrspecdeviserspec-rails

RSpec Devise Session Testing


With rails 4.2.0 and the latest version of RSpec I generated a controller test.

How would I ensure my admin user is logged in?

For example: if current_user.admin?

In the rspec test it mentions it like so:

let(:valid_session) { {} }

How would I enter a valid session?


Solution

  • First you need to add the devise helpers in spec_helper file to be accessible in the tests, as mentioned in the wiki

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

    Then in the controller you could easily create a user object and sign it in using sign_in helper method