2016-06-13 6 views
1

に評論家0.3から移動するときにポリシーを見つけることができません。しかし、以前のバージョンの専門家(0.3)に切り替えると、すべて正常に動作します。評論家:: NotDefinedError:私は私は私が前に見ていない複数のエラーを取得する私のプロジェクトの仕様クラスのいずれかにRSpecのウィット<strong>評論家バージョン1.0</strong>を実行している場合は1.0

今まで私が気づいたことは、作成関数の新しいバージョンのperit @errorが正しく割り当てられていないことです(エラークラスの代わりに、エラークラスからエラーメッセージ文字列が得られます)。

仕様/工場の
class ErrorsController < ApplicationController 
    before_action :set_execution_environment 

    def authorize! 
    authorize(@error || @errors) 
    end 
    private :authorize! 

    def create 
    @error = Error.new(error_params) 
    authorize! 
    end 

    def error_params 
    params[:error].permit(:message, :submission_id).merge(execution_environment_id: @execution_environment.id) 
    end 
    private :error_params 

:スペック/コントローラ/ error_controller.rbで

FactoryGirl.define do 
    factory :error, class: Error do 
    association :execution_environment, factory: :ruby 
    message "exercise.rb:4:in `<main>': undefined local variable or method `foo' for main:Object (NameError)" 
    end 
end 

describe 'POST #create' do 
    context 'with a valid error' do 
     let(:request) { proc { post :create, execution_environment_id: FactoryGirl.build(:error).execution_environment.id, error: FactoryGirl.attributes_for(:error), format: :json } } 

     context 'when a hint can be matched' do 
     let(:hint) { FactoryGirl.build(:ruby_syntax_error).message } 

     before(:each) do 
      expect_any_instance_of(Whistleblower).to receive(:generate_hint).and_return(hint) 
      request.call 
     end 

     expect_assigns(execution_environment: :execution_environment) 

     it 'does not create the error' do 
      allow_any_instance_of(Whistleblower).to receive(:generate_hint).and_return(hint) 
      expect { request.call }.not_to change(Error, :count) 
     end 

     it 'returns the hint' do 
      expect(response.body).to eq({hint: hint}.to_json) 
     end 

     expect_json 
     expect_status(200) 
     end 

     context 'when no hint can be matched' do 
     before(:each) do 
      expect_any_instance_of(Whistleblower).to receive(:generate_hint).and_return(nil) 
      request.call 
     end 

     expect_assigns(execution_environment: :execution_environment) 

     it 'creates the error' do 
      allow_any_instance_of(Whistleblower).to receive(:generate_hint) 
      expect { request.call }.to change(Error, :count).by(1) 
     end 

     expect_json 
     expect_status(201) 
     end 
    end 

私はエラークラスので、エラーメッセージ

Pundit::NotDefinedError: unable to find policy Pundit::ErrorPolicy for #<Pundit::Error: {"message"=>"exercise.rb:4:in ': undefined local variable or method foo' for main:Object (NameError)", "execution_environment_id"=>1}>

を取得正しく作成されていません。その後、エラークラスのすべてのテストは失敗します。

私のポリシーは:

class AdminOrAuthorPolicy < ApplicationPolicy 
    [:create?, :index?, :new?].each do |action| 
    define_method(action) { @user.internal_user? } 
    end 

    [:destroy?, :edit?, :show?, :update?].each do |action| 
    define_method(action) { admin? || author? } 
    end 
end 


class ErrorPolicy < AdminOrAuthorPolicy 
    def author? 
    @user == @record.execution_environment.author 
    end 
end 

私は他のクラスとは、このような問題を持っていません。

答えて

0

私はminitestを使用していますが、過去30分間同じ問題を処理していました。解決策はspring stopを実行してからテストを再実行することでした。お役に立てれば。

+0

残念ながら、「スプリングストップ」は私の場合は何も変わりません。 – yqbk

関連する問題