[英]Testing nested CanCan abilities with RSpec
I spent a bit of time digging through CanCan to get nested resources working. 我花了一些时间挖掘CanCan以获得嵌套资源。 It works as expected in the browser, but I can't get the related ability spec to pass. 它在浏览器中按预期工作,但我无法通过相关的能力规范。 I'm guessing it has something to do with how CanCan handles nested routes. 我猜它与CanCan如何处理嵌套路由有关。 Any suggestions on how to properly test the failing abilities (marked below)? 关于如何正确测试失败能力的任何建议(标记如下)? Thanks. 谢谢。
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
The ability class defines the following related to reading networks and managing affiliations. 能力类定义以下与阅读网络和管理从属关系相关的内容。 The NetworkManagementPolicy class returns true/false based on certain criteria and works as expected. NetworkManagementPolicy类根据特定条件返回true / false,并按预期工作。 Even when removing the calls to this class and hard returning true/false, I can't get the abilities specs to pass. 即使删除对此类的调用并且难以返回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
We need more specifics about your models and the "can_read?" 我们需要更多关于你的模型和“can_read?”的细节。 conditions to properly answer this question. 正确回答这个问题的条件。 I am not sure how your Affiliation model relates to the NetworkManagementPolicy or Network. 我不确定您的Affiliation模型与NetworkManagementPolicy或Network的关系。
However, given that a network has_many network_management_polices and that each affiliation is attached to a user and network with standard rails conventions this should get you moving. 但是,鉴于网络具有多个network_management_polices并且每个联盟都附加到具有标准rails约定的用户和网络,这应该让您感动。
class NetworkManagementPolicyController < ApplicationController
load_and_authorize_resource :network
load_and_authorize_resource :network_management_policy, through: :network
end
can :manage, NetworkManagementPolicy, network: { affiliations: { user_id: current_user.id} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.