【问题标题】:Rails Pundit RSpecRails Pundit RSpec
【发布时间】:2017-11-04 01:41:01
【问题描述】:

我将 Pundit 与 Devise 结合使用,但我认为我在 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  'test@example.com'
    # 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

还有一个 Profile 工厂

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

我知道我错过了一些简单的东西..所以会得到一个duh!稍后。

【问题讨论】:

  • 我想我明白了。在我的策略方法中,我将其更改为 .. "@current_user == @profile_user" 并且它起作用了。
  • Laurie,继续发布您的解决方案作为答案,这样像我这样的人不会试图帮助您,只是意识到您已经解决了它。

标签: rspec factory-bot pundit


【解决方案1】:

在我的 profile_policy.rb 中,我将 owner_of_record 更改为

类 ProfilePolicy ....

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

结束

【讨论】:

    猜你喜欢
    • 2014-05-18
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    相关资源
    最近更新 更多