1
我有了一個控制器,而不處理報表模型/對象Rails應用程序:測試慘慘使用RSpec查看網頁的控制器不使用模型的能力
# reports_controller.rb
class ReportsController < ApplicationController
authorize_resource :class => false
def index
end
def report_title
# etc.
end
# etc.
end
用戶對象可以有兩種作用:管理員,普通用戶或查看者。管理員可以做任何事情,普通用戶有一系列的規則,觀衆不能對任何應用的對象做任何事情,但他們可以查看所有的報告。
# ability.rb
def initialize(user)
if user.admin?
can :manage, :all
elsif user.normal?
# stuff according to business rules...
elsif user.viewer?
can [:index, :report_title, ...], :report
end
end
這按預期工作(所有其他控制器的有load_and_authorize_resource
),但我怎麼去單元測試ability.rb這些行?我可以在ability_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
這很好,謝謝。 – australis