2017-01-11 2 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 

これは、(RSpecのから)私のrails_helperです:

# 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ヘルパーセクションをチェックしてください:http://devise.plataformatec.com.br/#test-helpers – MoskeyOmbus

+0

@MoskeyOmbus私はdeviseテストヘルパーを使用しています。質問は、controller、ability、およびrails_helperコードで更新されます。 – rwehresmann

答えて

1

nil:NilClassエラーが発生する主な理由は、Devise経由でログインしていない限り、がnilであるためです。

def initialize(user) 
    return unless user.present? 

    if user.teacher? 
    ... 
    end 
end 

あなたはあなたの能力からnilを返す場合、認証に失敗し、それに応じて処理されます。ユーザーは、このようなものをログインしていないとき、あなたの能力がために処理する必要があります。

+0

私は@MoskeyOmbusを参照してください。しかし、これを追加したり、新しいユーザを設定した場合には新しいユーザが設定されたりすると、リダイレクトが発生しないため(実際のケースが起こるためテストでのみ)、エラーが発生したままです。現時点では、私はそれに対処する正しい方法が何であるかのアイデアはありません。また、私は今、 'current_user'がDevise' sign_in'ヘルパーを使用してもnilであることを見ています(しかし、これは別の質問の問題です)。 – rwehresmann

+0

私はこの答えを受け入れます。私はいくつかの細部を渡して、質問の説明で渡した情報に基づいて、@MoskeyOmbusで指し示されたものだけを特定することができました。 – rwehresmann

関連する問題