【问题标题】:Devise before_action :authenticate_user! not working设计 before_action :authenticate_user!不工作
【发布时间】:2014-11-29 15:46:28
【问题描述】:

我正在使用设计以及要添加什么authenticate_user!在以下控制器中:

class RegistrationsController < Devise::RegistrationsController
  before_action :authenticate_user!

  def update
    @user = User.find(current_user.id)
    if @user.update(user_params)
      sign_in @user, :bypass => true
      redirect_to edit_user_registration_path, notice: 'Your account has been updated.'
    else
      render :edit
    end
  end

private
  def user_params
    params.require(:user).permit(:first_name, :last_name, :email)
  end
end

路线是:

devise_for :users, controllers: { omniauth_callbacks: "authentications", 
                                  registrations: 'registrations' }

我有以下测试来验证身份验证是否有效:

require 'spec_helper'

describe RegistrationsController do
  context 'Without being a signed in user' do
    describe 'PATCH #update' do
      let! (:user) { create(:user) }

      it 'gets the correct flash message' do
        patch :update, user: attributes_for(:user)
        flash[:alert].should eql('You need to sign in or sign up before continuing.')
      end

      it 'redirects to the sign in page' do
        patch :update, user: attributes_for(:user)
        response.should redirect_to(new_user_session_path)
      end
    end
  end
end

我有追随者用户工厂:

FactoryGirl.define do
  factory :user do
    first_name 'John'
    last_name 'Smith'
    sequence(:email) { |n| "John#{n}@example.com" }
    password 'pw'
  end
end

运行规范时出现以下错误:

Failures:

  1) RegistrationsController Without being a signed in user PATCH #update redirects to the sign in page
     Failure/Error: patch :update, user: attributes_for(:user)
     NoMethodError:
       undefined method `name' for nil:NilClass
     # ./spec/controllers/registrations_controller_spec.rb:14:in `block (4 levels) in <top (required)>'

  2) RegistrationsController Without being a signed in user PATCH #update gets the correct flash message
     Failure/Error: patch :update, user: attributes_for(:user)
     NoMethodError:
       undefined method `name' for nil:NilClass
     # ./spec/controllers/registrations_controller_spec.rb:9:in `block (4 levels) in <top (required)>'

before_action :authenticate_user!在应用程序的其他部分正常工作。

【问题讨论】:

  • 请向用户出示您的factory
  • 我添加了用户工厂。谢谢。
  • 嗯,看起来很奇怪,你能在patch :update, user: attributes_for(:user)之前加puts attributes_for(:user)吗?
  • {:first_name=&gt;"John", :last_name=&gt;"Smith", :email=&gt;"John2@example.com", :password=&gt;"pw"}
  • 不确定这是否是个问题,但在设计默认验证 password 时,如果您不更改它,它应该是 8 个字符。

标签: ruby-on-rails rspec devise


【解决方案1】:

测试似乎失败了,因为我没有让 Devise 知道要使用哪个映射,我假设这意味着它正在测试不同控制器的更新方法。

Per the Devise docs: "如果你正在测试 Devise 内部控制器或继承自 Devise 的控制器,你需要在请求之前告诉 Devise 应该使用哪个映射。这是必要的,因为 Devise 从路由器获取此信息,但是由于功能测试不通过路由器,因此需要明确告知。”

我需要在规范中添加@request.env["devise.mapping"] = Devise.mappings[:user]

以下规范有效:

  context 'Without being a signed in user' do
    describe 'PATCH #update' do 
      it 'gets the correct flash message' do
        @request.env["devise.mapping"] = Devise.mappings[:user]
        patch :update, user: { first_name: 'Steve', last_name: "Smith" }
        flash[:alert].should eql('You need to sign in or sign up before continuing.')
      end

      it 'redirects to the sign in page' do
        @request.env["devise.mapping"] = Devise.mappings[:user]
        patch :update, user: { first_name: 'Steve', last_name: "Smith" }
        response.should redirect_to(new_user_session_path)
      end
    end
  end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    相关资源
    最近更新 更多