簡體   English   中英

測試控制器時,cancancan能力初始化器中的錯誤

[英]Error in cancancan ability initializer when testing controllers

我在應用程序中添加了cancancan gem,但它破壞了我所有的控制器和功能測試。 錯誤在ability.rb初始化中初始化: user變量為nil。

這是其中一項測試的錯誤日志:

14) TestCasesController DELETE #destroy when not logged-in redirects to sign-in page
      Failure/Error: if user.teacher?

      NoMethodError:
        undefined method `teacher?' for nil:NilClass
      # ./app/models/ability.rb:5:in `initialize'
      # /usr/local/rvm/gems/ruby-2.3.1/gems/cancancan-1.15.0/lib/cancan/controller_additions.rb:361:in `new'
      # /usr/local/rvm/gems/ruby-2.3.1/gems/cancancan-1.15.0/lib/cancan/controller_additions.rb:361:in `current_ability'
      # /usr/local/rvm/gems/ruby-2.3.1/gems/cancancan-1.15.0/lib/cancan/controller_additions.rb:342:in `authorize!'
      # /usr/local/rvm/gems/ruby-2.3.1/gems/cancancan-1.15.0/lib/cancan/controller_resource.rb:49:in `authorize_resource'
      # /usr/local/rvm/gems/ruby-2.3.1/gems/cancancan-1.15.0/lib/cancan/controller_resource.rb:34:in `load_and_authorize_resource'
      # /usr/local/rvm/gems/ruby-2.3.1/gems/cancancan-1.15.0/lib/cancan/controller_resource.rb:10:in `block in add_before_action'
      # /usr/local/rvm/gems/ruby-2.3.1/gems/rails-controller-testing-1.0.1/lib/rails/controller/testing/template_assertions.rb:61:in `process'
      # /usr/local/rvm/gems/ruby-2.3.1/gems/devise-4.2.0/lib/devise/test/controller_helpers.rb:33:in `block in process'
      # /usr/local/rvm/gems/ruby-2.3.1/gems/devise-4.2.0/lib/devise/test/controller_helpers.rb:100:in `catch'
      # /usr/local/rvm/gems/ruby-2.3.1/gems/devise-4.2.0/lib/devise/test/controller_helpers.rb:100:in `_catch_warden'
      # /usr/local/rvm/gems/ruby-2.3.1/gems/devise-4.2.0/lib/devise/test/controller_helpers.rb:33:in `process'
      # /usr/local/rvm/gems/ruby-2.3.1/gems/rails-controller-testing-1.0.1/lib/rails/controller/testing/integration.rb:12:in `block (2 levels) in <module:Integration>'
      # ./spec/controllers/test_cases_controller_spec.rb:182:in `block (3 levels) in <top (required)>'
      # ./spec/controllers/test_cases_controller_spec.rb:193:in `block (4 levels) in <top (required)>'

這是相應的操作:

  describe "DELETE #destroy" do
    let!(:test_case) { create(:test_case) }
    subject { delete :destroy, params: { id: test_case } }

    context "when not logged-in" do
      before { subject }

      it "redirects to sign-in page" do
        expect(response).to redirect_to(new_user_session_url)
      end
    end
  end

這是我的控制器:

class TestCasesController < ApplicationController
  include ApplicationHelper

  load_and_authorize_resource

  before_action :authenticate_user!
  before_action :find_test_case, only: [:show, :edit, :update, :destroy, :test]
  before_action :find_question, only: [:index, :new, :create, :test_all]

  def index
    @test_cases = TestCase.where(question: @question)
  end

  def new
    @test_case = TestCase.new
  end

  def create
    @test_case = @question.test_cases.build(test_case_params)
    if @test_case.save
      flash[:success] = "Caso de teste criado!"
      redirect_to @test_case
    else
      render 'new'
    end
  end

  def show
  end

  def edit
  end

  def update
    if @test_case.update_attributes(test_case_params)
      flash[:success] = "Caso de teste atualizado!"
      redirect_to @test_case
    else
      render 'edit'
    end
  end

  def destroy
    question = @test_case.question
    @test_case.destroy
    flash[:success] = "Caso de teste deletado!"
    redirect_to question_test_cases_url(question)
  end

  def test
    result = @test_case.test(plain_current_datetime, "pas", params[:source_code])
    @output = result[:output]
    @results = [ { title: @test_case.title, status: result[:status] } ]
    respond_to { |format| format.js }
  end

  def test_all
    @results = @question.test_all(plain_current_datetime, "pas", params[:source_code])
    respond_to { |format| format.js }
  end

    private

    def test_case_params
      params.require(:test_case).permit(:title, :description, :input, :output, :question_id)
    end

    def find_test_case
      @test_case = TestCase.find(params[:id])
    end

    def find_question
      @question = Question.find(params[:question_id])
    end
end

這是我的能力課:

Ability類包括CanCan :: Ability

  def initialize(user)
    if user.teacher?
      can :manage, [Exercise, Question, TestCase]
      can :manage, Team, owner: user
      can [:read, :create], Team
      can [:enroll], Team do |team|
        !team.enrolled?(user)
      end
      can [:unenroll], Team do |team|
        team.enrolled?(user)
      end
    end
  end
end

這是我的rails_helper(來自rspec):

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'

# Add additional requires below this line. Rails is not loaded until this point!
require 'support/helpers/relationships'

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!

  # Helpers
  config.include Helpers::Relationships, type: :model
  config.include Devise::Test::ControllerHelpers, :type => :controller
  config.include Warden::Test::Helpers
end

有關如何解決該問題的任何提示?

OBS :我正在使用Devise進行身份驗證,它為我提供了可以正常工作所需的current_user

出現nil:NilClass錯誤的主要原因是,除非您通過Devise登錄,否則current_usernil 您的能力需要處理用戶未登錄時的情況,如下所示:

def initialize(user)
  return unless user.present?

  if user.teacher?
    ...
  end
end

如果您從能力中返回nil ,則授權將失敗並進行相應處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM