현재 rspec에서 cancan을 사용 중입니다. 내가 전무로 @ability을 얻고 왜레일 초보자 테스트 CanQuan RSpec에 대한 질문
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_spec.rb
/내 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 파일에서 권한 제어와 관련된 컨트롤러의 일부 동작을 지정하려고합니다. 그게 가능하니? (내 응용 프로그램은 사용자의 3 개 역할이 있기 때문에 나는 명시 적으로 달성하고 싶은 나 자신이 내 컨트롤러가 하나 라이너와 관련된 모든 권한이있는 파일 스펙 넘쳐 찾을 수 있습니다.
감사합니다!
테스트에서 abiities를 사용하는 방법에 대한 지침을 따르지 않으므로. – sevenseacat
나는 CanCan ::을 꺼냈다. 이제는 http://stackoverflow.com/questions/6114857/why-is-my-cancanability-class-overly-permissive와 100 % 동일합니다. – disappearedng