0
私はDeviseでPunditを使用しており、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
01が必要です私は混乱しています
がcurrent_user.id ==でプロファイルを作成するために工場を取得していること..だから
undefined method `user' for nil:NilClass
よう そうでなければ、私は取得していますエラーをprofile.user_id私は、単純な何かが欠けています知っていますあなたは勇気を得ます!後で。私profile_policy.rbで
にowner_of_recordを変更しました。私の方針では、 "@ current_user == @profile_user"に変更しました。 – Laurie
Laurie、答えとしてあなたの解決策を投稿してください。私のような人はあなたがすでにそれを解決したことを理解するためだけに役立つことはありません。 – Daniel