Когда я запускаю rspec с pundit version 1.0 в одном из классов спецификации проекта, я получаю множество ошибок, которых раньше не видел. Однако при переходе на предыдущую версию pundit (0.3) все работает корректно.
До сих пор я заметил, что в более новой версии pundit @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
в спец/заводах:
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
в спецификации/контроллеры/error_controller.rb:
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: невозможно найти политику
Pundit::ErrorPolicy
для#<Pundit::Error: {"message"=>"exercise.rb:4:in
': неопределенная локальная переменная или метод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
У меня нет такой проблемы с любым другим классом.