1

I'm using a custom Devise SessionsController:

#custom_sessions_controller.rb
class CustomSessionsController < Devise::SessionsController
end

In my routes.rb devise is set up like this:

#routes.rb
devise_for :custom_users, { 
    :singular => 'custom_user',
    :class_name => 'CustomUser', 
    :path => "/", 
    :path_names => { :sign_in => 'login', :sign_out => 'logout' },
    :controllers => { :sessions => "custom_sessions" } 
}

I would like to write a simple rspec test:

#custom_sessions_controller_spec.rb
require 'rails_helper'
describe CustomSessionsController, :type => :controller do

  describe "login" do
    before do
      setup_controller_for_warden
      @request.env["devise.mapping"] = Devise.mappings[:custom_user]
      @my_user = FactoryGirl.create(:custom_user) # creates user with valid credentials
    end

    it "should succeed with valid credentials" do
      sign_in @my_user
      curr_user = assigns(:current_custom_user)

      expect(curr_user).to eq(@my_user)
      expect(response).to be_success
    end
  end
end

In my rails_helper.rb the following lines are present:

require 'devise'
...
RSpec.configure do |config|
  ...
  config.include Devise::TestHelpers, :type => :controller
  config.include Warden::Test::Helpers , :type => :controller
  ...

The problem is that curr_user in the test is alway nil. What am I doing wrong? Or how to test a custom devise session controller? Or how to log in in - other - tests using a custom devise session?

2 Answers 2

2

instead of:

describe CustomSessionsController, :type => :controller do

try:

RSpec.describe CustomSessionsController, :type => :controller do

Hope it works!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but unfortunately this didn't solve my issue.
There is none request in a action in your spec. Take a look at: relishapp.com/rspec/rspec-rails/v/3-3/docs/controller-specs to see how it properly works.
0

Using

@controller.current_custom_user

instead of

assigns(:current_custom_user)

seems to work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.