2017-01-11 16 views
0

我在我的應用程序中添加了cancancan gem,但它中斷了我的所有控制器和功能測試。 ability.rb初始化錯誤開始:user變量爲零。測試控制器時cancancan能力初始值設定器中的錯誤

這裏是一個測試的錯誤日誌:

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 

這是我的能力等級:

類能力 包括慘慘::能力

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

+0

無法看到控制器和能力代碼,我想你在進行測試請求時在技術上沒有'登錄',所以'load_and_authorize_resource'試圖查看你的當前用戶是否被授權,但是'current_user'是'nil'。 查看Devise文檔的Test Helpers部分:http://devise.plataformatec.com.br/#test-helpers – MoskeyOmbus

+0

@MoskeyOmbus我正在使用devise測試助手。問題用控制器,能力和rails_helper代碼更新。 – rwehresmann

回答

1

您收到nil:NilClass錯誤的主要原因是因爲current_usernil,除非您通過Devise登錄。你的能力需要處理當用戶沒有登錄,像這樣的:

def initialize(user) 
    return unless user.present? 

    if user.teacher? 
    ... 
    end 
end 

如果你從你的能力回報nil,授權失敗,並做相應的處理。

+0

我看,@MoskeyOmbus。但是,如果我添加了這個,或者事件設置了一個新用戶,如果不存在,我的測試仍然有錯誤,因爲重定向不會發生(僅在測試中,因爲在真實情況下發生)。目前,我不知道用什麼方法來解決這個問題。我也看到'current_user'是零,即使我使用Devise'sign_in' helper(但是,也許這是另一個問題的問題) – rwehresmann

+0

我會接受這個答案。我允許通過一些litlle細節,並根據我在問題描述中傳遞的信息,只有@MoskeyOmbus指出的信息才能被識別。 – rwehresmann