Rspec的。如何测试current_user方法

时间:2014-07-22 14:30:40

标签: ruby-on-rails rspec

我想从我的applicaton_controller中测试current_user方法:

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

但我收到了错误:

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: "anderson1@gmailmail.com", 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: "anderson1@gmailmail.com", 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 ==)

2 个答案:

答案 0 :(得分:1)

我假设您正在从Devise中包装current_user方法(您正在调用super)并且您正在使用devise test helper sign_in。它需要范围或用户对象作为其第一个参数。将测试中的before更改为:

before do
  sign_in :user, user
end

或只是:

before do
  sign_in user
end

Devise中的默认范围是:user,除非它有所不同,否则您无需告诉sign_in

答案 1 :(得分:0)

如果您在助手中定义了current_user方法并在控制器中调用它,则可以执行此类操作。

示例:

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

在测试文件中

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