Ruby on rails 测试CanCan在没有模型的控制器上使用RSpec查看页面的能力
我有一个Rails应用程序,它的控制器没有用于处理报告的模型/对象:Ruby on rails 测试CanCan在没有模型的控制器上使用RSpec查看页面的能力,ruby-on-rails,ruby,rspec-rails,cancan,cancancan,Ruby On Rails,Ruby,Rspec Rails,Cancan,Cancancan,我有一个Rails应用程序,它的控制器没有用于处理报告的模型/对象: # reports_controller.rb class ReportsController < ApplicationController authorize_resource :class => false def index end def report_title # etc. end # etc. end 这是按预期工作的(因为所有其他控制器都有加载\u和\
# reports_controller.rb
class ReportsController < ApplicationController
authorize_resource :class => false
def index
end
def report_title
# etc.
end
# etc.
end
这是按预期工作的(因为所有其他控制器都有加载\u和\u授权\u资源
),但如何在ability.rb中对这些行进行单元测试?我可以在Capability_spec.rb中与其他单元测试一起进行测试,还是只需通过请求进行测试
顺便说一下,通过请求进行测试确实有效,我只是想知道是否可以在这里进行测试
# ability_spec.rb
RSpec.describe User do
describe "abilities" do
subject(:ability){ Ability.new(user) }
CRUD = [:create, :read, :update, :destroy]
ALL_MODELS = # a list of models
# ...
context "a report viewer" do
let(:user){ FactoryGirl.create(:user, :viewer) }
it 'cannot do anything with any objects' do
ALL_MODELS.each do |object|
CRUD.each do |action|
should_not be_able_to(action, object)
end
end
end
it 'can access all the report actions' do
# ???
end
end
end
end
是的,您应该能够在
能力\u spec.rb
中测试该能力。我认为添加以下行可能有效:
it 'can access all the report actions' do
should be_able_to :index, ReportsController
should be_able_to :report_title, ReportsController
end
如果您设置了authorize\u resource:class=>ReportsController
而不是false
,并使用can[:index,:report\u title,…],:ReportsController
而不是符号:report
我还没试过,所以请告诉我是否有效
it 'can access all the report actions' do
should be_able_to :index, ReportsController
should be_able_to :report_title, ReportsController
end