Check if a user is signed out in devise

12,857

Solution 1

I think it's really what you need How To: Test controllers with Rails 3 and 4 (and RSpec)

Just check current_user. It should be nil

Add. Good practice is using syntax like this

-> { sign_out @admin }.should change { current_user }.from(@admin).to(nil)

Solution 2

it "should have a current_user" do
  subject.current_user.should_not be_nil
end

Found at https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-%28and-rspec%29

Solution 3

Not a new answer, really, but my rep isn't high enough to comment...:

  • If you've already overridden subject, the controller is available as controller in controller specs, so:

    expect { ... }.to change { controller.current_user }.to nil
    
  • To check for a specific user, say generated by FactoryGirl, we've had good success with:

    let(:user) do FactoryGirl.create(:client) ; end
    ...
    it 'signs them in' do
        expect { whatever }.to change { controller.current_user }.to user
    end
    
    it 'signs them out' do
        expect { whatever }.to change { controller.current_user }.to nil
    end
    
Share:
12,857
Overtone
Author by

Overtone

Updated on June 05, 2022

Comments

  • Overtone
    Overtone almost 2 years

    I'm trying to check if an administrator is signed out in an Rspec test. However the usual signed_in? method can't be seen from rspec and isn't part of the RSpec Devise Helpers.

    Something like this is what i have in place

    before (:each) do
            @admin = FactoryGirl.create(:administrator)
            sign_in @admin
          end
    
    
          it "should allow the admin to sign out" do
            sign_out @admin
            #@admin.should be_nil
            #@admin.signed_in?.should be_false
            administrator_signed_in?.should be_false
          end
    

    Is there anothe way to check the session of the administrator and see if he's actually signed in or not?

  • Overtone
    Overtone almost 12 years
    indeed. I had just found subject.current_administrator.should be_nil in some old ruby code. I had no idea of subject before that. Thank you.
  • Overtone
    Overtone almost 12 years
    In my case for your example, would it not be current_administrator as administrator is the model used and not user?
  • caulfield
    caulfield almost 12 years
    current_user is devise helper. it refers to the signed user in current session, because you cannot sign in twice at the same time
  • Overtone
    Overtone almost 12 years
    undefined local variable or method `current_user'... current_administrator works though. Odd.
  • Overtone
    Overtone almost 12 years
    Also, appreciate the link, but I had already read it and your 'current_user' variable was what stuck out and helped me solve this.
  • Rebekah Waterbury
    Rebekah Waterbury over 11 years
    This only tests the devise test helper methods sign_in and sign_out. What if I want to test that something in the site actually signs in a user or signs out a user? The current_user var is not available if I create a user with a Factory and then using Capabara capabilities log the user in. Any thoughts?
  • Bruno Buccolo
    Bruno Buccolo over 8 years
    Use expect(subject.current_user).to eq your_expectation for RSpec 3 syntax.