用CanCan进行RSpec的Rails初学者测试问题(Rails Beginner Testing Question for RSpec with CanCan)

我目前正在使用带有rspec的cancan。

请看看我的能力.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

在我的spec / models / 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个用户角色,我发现自己乱丢我的控制器规格文件,所有这些权限都与一个内衬相关。

谢谢!

I am currently using cancan with rspec.

Please take a look at my 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

In my spec/models/ability_spec.rb I have

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

Any ideas why I am getting @ability as nil?

In addition, I want to put some of my controller's actions that are related to permission control in this ability_spec.rb file. Is that possible? (I explicitly want to achieve this because my app has 3 roles of users and I find myself littering my controllers spec files with all these permission related one liners.

Thanks!

最满意答案

测试必须出现在it或specify块。 describe和context仅用于分组。

describe "success" do #**This line I am getting ability is nil @ability.should == 5 end

应该更像是:

it "allows consumers to do blah blah blah" do @ability.should == 5 end

Tests must appear in it or specify blocks. describe and context are simply for grouping.

describe "success" do #**This line I am getting ability is nil @ability.should == 5 end

Should be more like:

it "allows consumers to do blah blah blah" do @ability.should == 5 end

更多推荐