2017-11-04 185 views
0

我正在使用權威與設計,並認爲我錯過了一些RSpec很基本的東西。Rails Pundit RSpec

我有一個簡單的政策,讓擁有該模型的用戶查看它。 但我不知道如何爲它寫一個FactoryBot工廠。

我的政策工作正常,當我在瀏覽器中測試它。

class ProfilePolicy 
    # class Scope < Scope 
    # def resolve 
    #  scope.where(user_id :@user.try(:id)) 
    # end 
    # end 

    attr_reader :current_user, :model 

    def initialize(current_user, model) 
    @current_user = current_user 
    @profile = model 
    end 


    def index? 
    @current_user.has_role? :admin 
    end 

    def show? 
    user_is_owner_of_record 
    end 

    def create? 
    @current_user_user.has_role? :seller 
    end 

    def new? 
    create? 
    end 

    def update? 
    user_is_owner_of_record 
    end 

    def edit? 
    update? 
    end 

    def destroy? 
    user_is_owner_of_record 
    # false 
    end 

    private 
    def user_is_owner_of_record 
    @user == @record.user 
    end 
end 

我有一個用戶工廠

FactoryBot.define do 
    factory :user do 
    sequence(:email) { |n| "user_#{n}@factory.com" } 
    # email '[email protected]' 
    # email '#{firstname}@#{lastname}.com' 
    password 'password' 
    password_confirmation 'password' 
    firstname 'test' 
    lastname 'example' 
    confirmed_at Time.now 

    # last_signed_in {10.days.ago} 

    trait :admin do 
     after(:create) {|user| user.add_role :admin} 
     after(:stub) {|user| user.add_role :admin} 
    end 

    trait :seller do 
     after(:create) {|user| user.add_role :seller} 
     after(:stub) {|user| user.add_role :seller} 
    end 


    # trait :profile do 
    # after(:create) {|user| user.profile_create(overview: "test", name: "test", account_authorized: true) 
    # end 
    end 
end 

和型材廠

FactoryBot.define do 
    factory :profile do 

    name "test" 

    overview "test" 
    account_authorized true 
    association :current_user, factory: :user 
    end 


end 

和政策規格低於

需要 'rails_helper'

describe ProfilePolicy do 
    subject { described_class } 

    let (:current_user) { FactoryBot.build_stubbed :user } 
    let (:admin) { FactoryBot.build_stubbed :user, :admin } 
    let (:profile) { current_user.build_profile(name: "test", overview: "test", account_authorized: true) } 

    permissions :index? do 
    it 'denies access if not an admin' do 
     expect(subject).not_to permit(current_user) 
    end 
    it 'allows access if admin' do 
     expect(subject).to permit(admin) 
    end 
    end 
    THIS IS WHERE I'm STUCK 
    **permissions :show? do 

    it 'allows access if record owner' do 
     expect(subject).to permit(current_user, profile) 
    end 
    end** 
end 

當我感到困惑越來越工廠創建具有current_user.id ==輪廓profile.user_id 否則,我得到這樣的錯誤

undefined method `user' for nil:NilClass 

我知道我失去了一些東西簡單..所以會得到一個!後來。

+0

我想我想通了。在我的策略方法中,我將其更改爲..「@current_user == @profile_user」,它工作正常。 – Laurie

+0

Laurie,請繼續併發布您的解決方案作爲答案,以便像我這樣的人不試圖幫助您,只是意識到您已經解決了它。 – Daniel

回答

0

在我profile_policy.rb我改變了owner_of_record到

類ProfilePolicy ....

private 
    def user_is_owner_of_record 
    @current_user == @profile.user 
    end