Mike Andrianov
Mike Andrianov

Reputation: 3085

Rspec. How to test current_user method

I want to test current_user method from my applicaton_controller:

describe ApplicationController do
  let(:user) { build(:user) }

  context "when authorized" do
      before do
        sign_in :current_user, user
      end

    it "#current_user" do
      expect(controller.send(:current_user)).to eq(user)
    end
end

application_controller:

  protected
  def current_user
    super || (@guest ||= Guest.new(session))
  end

but I've get an error:

Failure/Error: expect(controller.send(:current_user)).to eq(user)

   expected: #<Users::User id: nil, email: "", encrypted_password: "$2a$04$Xp6Xl33OQ5xoV6w8t9wgtuxQ4ngmN40yBFAHUNKVvCp...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, name: "Mr Anderson 1", phone: nil, phone_confirmation_token: "ABCDE", phone_confirmation_sent_at: nil, phone_confirmed_at: nil, unconfirmed_phone: "+375291234567", email_confirmation_token: nil, email_confirmed_at: nil, email_confirmation_sent_at: nil, unconfirmed_email: "[email protected]", phone_confirmation_attempts_count: 0, email_confirmation_attempts_count: 0>
        got: #<Users::NullUser:0x007fea0d15aed8 @session={"warden.user.current_user.key"=>#<Users::User id: nil, email: "", encrypted_password: "$2a$04$Xp6Xl33OQ5xoV6w8t9wgtuxQ4ngmN40yBFAHUNKVvCp...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, name: "Mr Anderson 1", phone: nil, phone_confirmation_token: "ABCDE", phone_confirmation_sent_at: nil, phone_confirmed_at: nil, unconfirmed_phone: "+375291234567", email_confirmation_token: nil, email_confirmed_at: nil, email_confirmation_sent_at: nil, unconfirmed_email: "[email protected]", phone_confirmation_attempts_count: 0, email_confirmation_attempts_count: 0>, "users/null_user"=>{}}, @id=nil, @email=nil, @encrypted_password=nil, @reset_password_token=nil, @reset_password_sent_at=nil, @remember_created_at=nil, @sign_in_count=nil, @current_sign_in_at=nil, @last_sign_in_at=nil, @current_sign_in_ip=nil, @last_sign_in_ip=nil, @created_at=nil, @updated_at=nil, @name=nil, @phone=nil, @phone_confirmation_token=nil, @phone_confirmation_sent_at=nil, @phone_confirmed_at=nil, @unconfirmed_phone=nil, @email_confirmation_token=nil, @email_confirmed_at=nil, @email_confirmation_sent_at=nil, @unconfirmed_email=nil, @phone_confirmation_attempts_count=nil, @email_confirmation_attempts_count=nil, @password=nil, @identities=[], @site_config=nil, @phone_token_offer=nil, @email_token_offer=nil>

   (compared using ==)

Upvotes: 2

Views: 1930

Answers (2)

Prabhakar
Prabhakar

Reputation: 6754

You can do something like this, if you've current_user method defined in your helper and calling it in the controller.

Example :

 @todos = current_user.todos.new("something")

in test file

expect(current_user).to be_present
expect(user_signed_in?).to be_true

Upvotes: 0

DiegoSalazar
DiegoSalazar

Reputation: 13521

I'm assuming you're wrapping the current_user method from Devise (you're calling super) and you're using the devise test helper sign_in. It needs the scope or a user object as its first argument. Change your before in your test to:

before do
  sign_in :user, user
end

or just:

before do
  sign_in user
end

The default scope in Devise is :user and you don't need to tell sign_in unless it's different.

Upvotes: 1

Related Questions