Search code examples
ruby-on-railsdeviserspec-railsruby-on-rails-5.2

Devise sign_in does not work in controller specs


It seems, that devise's sign_in helper doesn't work.

The regular sign in procedure works as a charm, tested via integration test and by filling the login form manually. Both methods delivered the correct value for current_user.

I wanted to test the user profile's edit controller functions and ran into a 'not authorized' issue. That led me to the current specs, where I just wanted to approve that there actually is no user signed in and, therfore, current_user is nil.

I suppose, that there is just something missig in my code.

Do you have any hints to solve my problem?

I'm using Rails 5.2 and ruby 2.5.0

models/user.rb

class User < ApplicationRecord
  enum role: [:registered, :editor, :admin]
  after_initialize :set_default_role, if: :new_record?

  def set_default_role
    self.role ||= :registered
  end

  # Include default devise modules. Others available are:
  devise :invitable, :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

controllers/application_controller.rb

class ApplicationController < ActionController::Base

  before_action :authenticate_user!
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name,  :last_name, :approved])
  end
end

controllers/registration_controller.rb

class RegistrationsController < Devise::RegistrationsController

  private

  def after_inactive_sign_up_path_for(resource)
    show_post_register_info_path
  end   
end

config/route.rb

Rails.application.routes.draw do

  devise_for :users, controllers: { registrations: 'registrations'}

  root to: 'welcome#home
  get 'show_post_register_info', to: 'static_pages#show_post_register_info'
end

specs/rails_heper.rb

require 'rspec/rails'
require 'devise'

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

RSpec.configure do |config|
  config.include Devise::Test::ControllerHelpers, type: :controller
  config.include Devise::Test::ControllerHelpers, type: :view
  config.include Devise::Test::IntegrationHelpers
  config.extend ControllerMacros, type: :controller
  ...
end

support/controller_macros.rb

module ControllerMacros
  def login_user
    before(:each) do
        @request.env["devise.mapping"] = Devise.mappings[:user]
        user = FactoryBot.create(:user)
        sign_in user
    end
  end
end

and, finally, my controller_spec:

require 'rails_helper'

RSpec.describe RegistrationsController, type: :controller do

describe 'registrations#update'  do

    context 'signing in a registered user' do
      login_user

      it 'should have a current_user' do
        expect(subject.current_user).to_not eq(nil)
      end

      it 'should be signed in' do
        expect(subject.user_signed_in?).to be true
      end
    end
  end
end

Result:

1) RegistrationsController registrations#update as a registered user changes the password
 Failure/Error: expect(subject.user_signed_in?).to be true

   expected true
        got false

2) RegistrationsController registrations#update as a registered user should have a current_user
 Failure/Error: expect(subject.current_user).to_not eq(nil)

   expected: value != nil
        got: nil

Solution

  • In Specs/support/controller_macros.rb I edited the login procedure a bit:

    def login_user
      before  do
        @request.env["devise.mapping"] = Devise.mappings[:user]
        user = FactoryBot.create(:user)
        subject.sign_in user # was sign_in user
      end
    end
    

    I really don't know why I have to call the sign_in method from a controller object (controller.sign_in works, too), but I'm satisfied with this.

    Maybe somenone wants to explain, why it must be subject.sign_in user.