Ich benutze Pundit mit Devise und denke, dass ich etwas ziemlich Grundlegendes zu RSpec vermisse.Rails Pundit RSpec
Ich habe eine einfache Richtlinie, mit der ein Benutzer, der das Modell besitzt, es anzeigen kann. Aber ich habe keine Ahnung, wie man eine FactoryBot-Fabrik dafür schreibt.
Meine Richtlinie funktioniert gut, wenn ich es im Browser testen.
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
Ich habe einen Benutzer Fabrik
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
und ein Profil Fabrik
FactoryBot.define do
factory :profile do
name "test"
overview "test"
account_authorized true
association :current_user, factory: :user
end
end
und die Politik Spec unter
require '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
Wo ich bin verwirrt ist, um die Fabriken immer ein Profil mit dem current_user.id erstellen == profile.user_id Ansonsten erhalte ich Fehler wie
undefined method `user' for nil:NilClass
Ich weiß, dass ich etwas einfach fehle .. so wird ein duh bekommen! später.
Ich glaube, ich es herausgefunden. In meiner Methode zu meiner Richtlinie habe ich sie in "@current_user == @profile_user" geändert und es hat funktioniert. – Laurie
Laurie, mach weiter und poste deine Lösung als Antwort, damit jemand wie ich dir nicht helfen will, nur um zu erkennen, dass du es bereits gelöst hast. – Daniel