2012-02-16 14 views
3

私はCanCanの能力をはじめてテストしており、困惑しています。私は何かを見逃しています...たとえ私が缶の中の虚偽/真実を返すとしても:invite_toブロック私はまだ仕様を通過していません。 CanCanのマッチャーを使っていないのですか?またはスタブ? CanCanで能力を定義するか?RSpecでCanCanの能力を正しくテストするには

何か不足していますか?

class Ability 
    include CanCan::Ability 

    def initialize(user) 
    user ||= User.new 

    can :invite_to, Network do |network| 
     network.allows_invitations? && (user.admin? || user.can_send_invitations_for?(network)) 
    end 
    end 
end 

ability_spec.rb

require 'cancan' 
require 'cancan/matchers' 
require_relative '../../app/models/ability.rb' 

class Network; end; 

describe Ability do 
    let(:ability) { Ability.new(@user) } 

    describe "#invite_to, Network" do 
    context "when network level invitations are enabled" do 
     let(:network) { stub(allows_invitations?: true) } 

     it "allows an admin" do 
     @user = stub(admin?: true) 
     ability.should be_able_to(:invite_to, network) 
     end 

     it "allows a member if the member's invitation privileges are enabled" do 
     @user = stub(admin?: false, can_send_invitations_for?: true) 
     ability.should be_able_to(:invite_to, network) 
     end 

     it "denies a member if the member's invitation privileges are disabled" do 
     @user = stub(admin?: false, can_send_invitations_for?: false) 
     ability.should_not be_able_to(:invite_to, network) 
     end 
    end 
    end 
end 

障害

1) Ability#invite_to, Network when network level invitations are enabled allows an admin 
    Failure/Error: ability.should be_able_to(:invite_to, network) 
     expected to be able to :invite_to #<RSpec::Mocks::Mock:0x3fe3ed90444c @name=nil> 
    # ./spec/models/ability_spec.rb:16:in `block (4 levels) in <top (required)>' 

    2) Ability#invite_to, Network when network level invitations are enabled allows a member if the member's invitation privileges are enabled 
    Failure/Error: ability.should be_able_to(:invite_to, network) 
     expected to be able to :invite_to #<RSpec::Mocks::Mock:0x3fe3edc27408 @name=nil> 
    # ./spec/models/ability_spec.rb:21:in `block (4 levels) in <top (required)>' 

答えて

4
let(:network) do 
    n = Network.new 
    n.stub!(:allows_invitations? => true) 
    n 
    end 
ability.rb

コードを記述したときにコードを実行すると、Canブロック内のコードに決して到達しません。スタブへの呼び出しはRSpec :: Mocks :: Mockクラスのオブジェクトを返します。ルールを適用するには、CanCanのクラスNetworkである必要があります。

+0

優れたキャッチ。インスタンスの代わりにクラスが与えられたときにブロックをスキップすることについて、ドキュメントのその部分を読んだだけです。しかし、それはクリックしませんでした。ありがとう。 –

関連する問題