2015-02-04 30 views
1

任何人都可以幫助我理解爲什麼只有我的否定(即should_not)測試會失敗?我所有的should測試都通過了,所以它讓我覺得我的某些配置錯誤,儘管我不知道。RSpec和Cancancan:所有負面測試失敗

ability.rb

# encoding: utf-8 
# 
class Ability 
    include CanCan::Ability 

    def initialize(user) 
    @user = user || User.new 
    if @user.administrator? 
     administrator_abilities 
    elsif @user.client? 
     client_abilities 
    elsif @user.guest? 
     guest_abilities 
    end 
    can :read, :share 
    end 

    private 

    def administrator_abilities 
    can :manage, :all 
    can [:index, :filter, :new, :create], :administration 
    end 

    def client_abilities 
    cannot :manage, administrator_resources 
    can :manage, [:account, :provider_auth, :user_auth] 
    can :read, [Component, Cover, Introduction, Template] 
    can :manage, client_made_resources, user_id: @user.id 
    end 

    def guest_abilities 
    cannot :manage, client_made_resources 
    cannot :manage, administrator_resources 
    can :create, Expression 
    end 

    def client_made_resources 
    [Authorisation, Document, Component, 
    CustomArticle, EditedArticle, Invoice, Order, Photo] 
    end 

    def administrator_resources 
    [Brand, Chart, Component, Cover, Expression, Introduction, Mode, 
    Preference, Price, Template, User, UserNote] 
    end 
end 

user_spec.rb

# encoding: utf-8 
# 
require 'spec_helper' 
require 'cancan/matchers' 
def client_made_resources 
    [Authorisation, Document, Component, CustomArticle, EditedArticle, Invoice, 
    Order, Photo] 
end 

def administrator_resources 
    [Brand, Chart, Component, Cover, Expression, Introduction, Mode, Preference, 
    Price, Template, User, UserNote] 
end 

RSpec.describe Ability do 
    let(:user) { create(:user, state: 'guest') } 
    subject { Ability.new(user) } 

    context 'guest' do 
    client_made_resources.each do |r| 
     it "should not be able to manage client's #{r}" do 
     expect(subject).to_not be_able_to(:manage, r.new) 
     end 
    end 

每個負試驗有這樣的結果:

rspec ./spec/models/user_spec.rb:69 # Ability guest should not be able to manage client's Document 

感謝您的幫助。

+0

對不起,我沒有你的答案。但是有一些提示:也許你應該將所有的cannots移動到一個首先運行的塊中?這將減少重複,並且在用戶通過所有角色檢查時更安全。同樣,一旦你說管理員「可以:管理,:所有」,就不需要額外的定義。 :管理涵蓋每一個行動,並且:涵蓋每一個對象。 – vpsz

+0

此外,如果您使資源列表中的類常量或類方法,您可以直接在測試中引用它們,而不必擔心保持兩個文件之間的列表同步。 – vpsz

+0

至於錯誤,你有沒有驗證user.administrator?和user.client?對你的分解對象實際上是錯誤的?我們需要看到模型和工廠完全知道發生了什麼。 – vpsz

回答

0

我不知道爲什麼,但我重構了我的測試,他們現在按預期工作。也許這會幫助別人。

RSpec.describe User do 
    describe 'Abilities' do 
    context 'guest' do 
     let(:user) { create(:user, state: 'guest') } 
     (client_made_resources + administrator_resources).each do |r| 
     it "cannot manage #{r}" do 
      ability = Ability.new(user) 
      assert ability.cannot?(:manage, r.new) 
     end 
     end 

     it 'can read shares' do 
     ability = Ability.new(user) 
     assert ability.can?(:read, :share) 
     end 
... 
+0

看起來可能問題在於be_able_to匹配器。如果您可以在簡單的應用程序中複製它,請提交錯誤報告。不過,看起來ryanb的cancan repo不再被維護。你可能想看看這個分支的更新版本:https://github.com/cancancommunity/cancancan – vpsz