入れ子のリソースを稼働させるために、少し時間をかけてCanCanを調べました。それはブラウザでは期待通りに動作しますが、私は関連する能力仕様を取得することはできません。 CanCanがどのようにネストされたルートを処理するかとは関係があると思います。失敗した能力を適切にテストする方法に関する提案(下記)ありがとう。RSpecで入れ子になったCanCanの能力をテストする
describe "Network" do
let(:network) { Network.new }
describe "#read" do
it "allows a user that meets the can_read? requirements" do
NetworkManagementPolicy.stub_chain(:new, :can_read?).and_return(true)
ability_for(user).should be_able_to(:read, network)
end
it "denies a user that does not meet the can_read? requirements" do
NetworkManagementPolicy.stub_chain(:new, :can_read?).and_return(false)
ability_for(user).should_not be_able_to(:read, network)
end
describe "Affiliation" do
let(:affiliation) { Affiliation.new }
describe "#manage" do
it "allows a user that meets the can_modify? requirements" do
# NOTE: Not sure why this is failing; Something to do with how
# CanCan handles nested resources?
#
# NetworkManagementPolicy.stub_chain(:new, :can_modify?).and_return(true)
# ability_for(user).should be_able_to(:manage, affiliation)
end
it "denies a user that does not meet the can_modify? requirements" do
NetworkManagementPolicy.stub_chain(:new, :can_modify?).and_return(false)
ability_for(user).should_not be_able_to(:manage, affiliation)
end
end
end
end
end
能力クラスは、ネットワークの読み取りと所属の管理に関連して以下を定義します。 NetworkManagementPolicyクラスは、特定の条件に基づいてtrue/falseを返し、期待どおりに動作します。このクラスへの呼び出しを取り除き、true/falseを返すことが難しい場合でも、私は能力仕様を得ることができません。
can :read, Network do |network|
can :manage, Affiliation do |affiliation|
NetworkManagementPolicy.new(network).can_modify?(current_user)
end
NetworkManagementPolicy.new(network).can_read?(current_user)
end
ブロック内で2番目の「can」を呼び出すことはできますか?私は[ブロック付き能力の定義](https://github.com/ryanb/cancan/wiki/Defining-Abilities-with-Blocks)wikiページにこのような例はありません。ブロックは、権限がチェックされるまで評価されません。 –