设计before_action:authenticate_user!不工作

时间:2014-11-29 15:46:29

标签: ruby-on-rails rspec devise

我正在使用设计和添加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!在他申请的其他部分正常工作。

1 个答案:

答案 0 :(得分:0)

看起来测试失败了,因为我没有让Devise知道使用哪个映射我假设它正在测试不同控制器的更新方法。

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

我需要在规范中添加@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
相关问题