加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用RSpec测试用户身份验证

发布时间:2020-12-17 02:22:19 所属栏目:百科 来源:网络整理
导读:我使用简单的rails 4.1.0应用程序,不使用设计.我正在测试应用程序控制器: class ApplicationController ActionController::Base protected def current_user @current_user ||= User.find(session[:user_id]) if session[:user_id] end def signed_in? !!cu
我使用简单的rails 4.1.0应用程序,不使用设计.我正在测试应用程序控制器:

class ApplicationController < ActionController::Base

  protected

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def signed_in?
    !!current_user
  end
  helper_method :current_user,:signed_in?

  def current_user=(user)
    @current_user = user
    session[:user_id] = user.try :id
  end

  # want to test this method
  def authenticate_user!
    render nothing: true,status: :unauthorized unless current_user
  end

end

我有authenticate_user的问题!方法.完整规格see here.方法规格:

describe 'authenticate_user!' do
    context 'when user logged in' do
      before { subject.send(:current_user=,create(:user)) }

      it 'do nothing' do
        expect(subject.send(:authenticate_user!)).to eq nil
      end
    end

    context 'when user not logged in' do
      controller do
        before_action :authenticate_user!
        def custom
          render :text => 'response'
        end
      end

      before do
        subject.send(:current_user=,nil)
        routes.draw { get 'custom' => 'anonymous#custom' }
        get :custom
      end
      it 'returns unauthorized status' do
        expect(response).to be_nil
      end
    #   before do
    #     subject.send(:current_user=,nil)
    #     subject.send(:authenticate_user!)
    #   end
    #
    #   it { render status: :unauthorized }
    #   it 'renders nothing' do
    #     expect(response).to be_nil
    #   end
    end

  end

它在用户登录时有效,但是当没有人时,我尝试了2种方法:匿名控制器,只是尝试调用此方法(在此处注释).此外,我试图从ApplicationController继承TestApplicationController,结果与匿名控制器相同.所以在这种情况下错误是下一个:

Failure/Error: expect(response).to be_nil
   expected: nil
        got: #<ActionController::TestResponse:0x007fc03b158580 @mon_owner=nil,@mon_count=0,@mon_mutex=#<Mutex:0x007fc03b0e3938>,@stream=#<ActionDispatch::Response::Buffer:0x007fc03b08bc10 @response=#<ActionController::TestResponse:0x007fc03b158580 ...>,@buf=[" "],@closed=false>,@header={"X-Frame-Options"=>"SAMEORIGIN","X-XSS-Protection"=>"1; mode=block",....

所以它返回一些响应,尽管current_user设置为nil并且有before_action:authenticate_user!在控制器中.我猜它忽略了它.

的情况下

before do
    subject.send(:current_user=,nil)
    subject.send(:authenticate_user!)
  end

  it { render status: :unauthorized }
  it 'renders nothing' do
    expect(response).to be_nil
  end

错误是下一个:

Failure/Error: subject.send(:authenticate_user!)
 Module::DelegationError:
   ActionController::RackDelegation#status= delegated to @_response.status=,but @_response is nil: #<ApplicationController:0x007fe854c592b8 @_action_has_layout=true,@_routes=nil,@_headers={"Content-Type"=>"text/html"},@_status=200,@_request=#<ActionController::TestRequest:0x007fe854c78c80 @env={"rack.version"=>[1,2],"rack.input"=>#<StringIO:0x007fe8544fbf98>,"rack.errors"=>#<StringIO:0x007fe8544f0080>,"rack.multithread"=>true,"rack.multiprocess"=>true,"rack.run_once"=>false,"REQUEST_METHOD"=>"GET","SERVER_NAME"=>"example.org","SERVER_PORT"=>"80",

它没有设置状态的响应对象.

此外,在这种情况下:

controller do
    # before_action :authenticate_user!
    def custom
      authenticate_user!
      render :text => 'response'
    end
  end

错误是不同的:

Failure/Error: render :text => 'response'
 AbstractController::DoubleRenderError:
   Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect,and at most once per action. Also note that neither redirect nor render terminate execution of the action,so if you want to exit an action after redirecting,you need to do something like "redirect_to(...) and return".

因此,渲染工作,但2次,它会引发错误.

我发现了很多种类似的测试,但主要是我设计的设计和方法.

感谢帮助.

解决方法

想通了.关键是 – 响应不应为零,因为动作回答了一些事情(渲染而不是空 – 空字符串).渲染状态::未授权错误,规范中没有渲染,它是来自控制器的一条线(我想知道它是如何出现的).所以这个方法的正确规范:

context "when user not logged in" do
  controller(ApplicationController) do
    before_action :authenticate_user!
    def custom
      render text: 'response'
    end
  end

  before do
    subject.send(:current_user=,nil)
    routes.draw { get 'custom' => 'anonymous#custom' }
    get 'custom'
  end

  it { should respond_with :unauthorized }
  it "returns nothing" do
    expect(response.body).to be_blank
  end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读