测试控制器时,cancancan能力初始化程序出错

时间:2017-01-11 00:28:00

标签: ruby-on-rails rspec devise cancancan

我在我的应用程序中添加了cancancan gem,但它破坏了我的所有控制器和功能测试。错误从ability.rb初始化开始:user变量为零。

以下是其中一项测试的错误日志:

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

这是我的能力课程:

班级能力   包括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进行身份验证,它为我提供了cancancan工作所需的current_user

1 个答案:

答案 0 :(得分:1)

您收到nil:NilClass错误的主要原因是current_usernil,除非您是通过Devise登录的。当用户未登录时,您的能力需要处理,如下所示:

def initialize(user)
  return unless user.present?

  if user.teacher?
    ...
  end
end

如果您从自己的能力中返回nil,则授权将失败并进行相应处理。