2017-09-21 12 views
0

私は、次のポリシーにRSpecの&評論家・マッチャテスト禁じ承認

module Admin 
    class PostPolicy < AdminPolicy 

    def index? 
     true 
    end 

    def show? 
     scope.where(:id => record.id).exists? && user.present? && user == record.user 
    end 

    def create? 
     true 
    end 

    def update? 
     user.present? && user == record.user 
    end 

    def destroy? 
     user.present? && user == record.user 
    end 

    def delete? 
     destroy? 
    end 

    end 
end 

を持っていると私はいくつかのRSpecのテストを書くことpundit-matchers宝石を使用しています。 permit authorization

RSpec.describe Admin::PostPolicy do 

    subject { described_class.new(user, [:admin, post]) } 
    let(:user) { FactoryGirl.create(:user) } 

    context 'permit authorization' do 
    let(:post) { FactoryGirl.create(:post, user_id: user.id) } 

    it { is_expected.to permit_action(:edit) } 
    it { is_expected.to permit_action(:delete) } 
    end 

    context 'deny authorization' do 
    let(:post) { FactoryGirl.create(:post, user_id: user.id + 1) } 

    it { is_expected.to forbid_action(:edit) } 
    it { is_expected.to forbid_action(:delete) } 
    end 
end 

テストは緑色ですが、私はdeny authorizationをテストしようとしたとき、私は私がテストを緑得るために、これを修正するにはどうすればよい次のエラー

ActiveRecord::RecordInvalid: 
     Validation failed: User must exist 

を取得しますか? - 私はあなたに想定しています

let(:post) { FactoryGirl.create(:post, user_id: user.id + 1) } 

有効ではありません。ここで指定したユーザーIDがあるため、ロジックは、テストが失敗している

答えて

0

ユーザーは自分の投稿を編集および削除することが許可されていることではないの他のユーザーポストモデルはあなたの投稿がユーザーに属しており、明らかにuser:user.id + 1は存在しません。

だけポストと一緒に作成される新しいユーザーを引き起こすことが

let(:post) { FactoryGirl.create(:post) } 

を行います。

関連する問題