1
我目前使用canpec和rspec。RSC CanCan的Rails初学测试问题
/请看看我的ability.rb
require 'spec_helper'
require "cancan/matchers"
class Ability
include CanCan::Ability
def initialize(user)
if user # Only logged in users
if user.role? :admin
can :manage, :all
elsif user.role? :producer
can :read, Business
can :update, Business do |b|
b.user_id == user.id
end
can :redeem, Purchase
elsif user.role? :consumer
can :your, Deal
can [:create, :redirect_to_wepay], Purchase
can :show, Purchase do |purchase|
purchase.user_id == user.id
end
end
# Good thing about devise with Cancan is that it takes care of this.
can :manage, User do |the_user|
the_user.id == user.id
end
else
# This is needed for the cans that follows
user = User.new
end
# Everyone's session
can :read, Deal
can :read, Business
# You have to enable it for wepay
can [:sold_out, :callback, :received], Purchase
end
end
在我的规格/型号ability_spec.rb我
describe Ability do
describe "consumers" do
describe "cancan" do
before(:each) do
@user = Factory(:user, :role => "consumer")
@ability = Ability.new(@user)
end
describe "success" do
#**This line I am getting ability is nil
@ability.should == 5
#**This line gives me be_able_to undefined
#@ability.should_not be_able_to(:read, Factory(:deal))
#@ability.can(:read, Factory(:business)).should be_true
end
任何想法,为什么我得到@ability作为零?
另外,我想在这个ability_spec.rb文件中放置一些与权限控制相关的控制器操作。那可能吗? (我明确要实现这一点,因为我的应用程序有用户3个角色,我发现自己乱扔我控制器符合规范与所有这些许可相关的一个衬垫的文件。
谢谢!
因为您没有按照关于如何在测试中使用优势的说明。 – sevenseacat
我已经拿出了CanCan ::。现在它是100%相同的http://stackoverflow.com/questions/6114857/why-is-my-cancan-ability-class-overly-permissive – disappearedng