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