ruby-on-rails - Rails Pundit Rspec 测试

标签 ruby-on-rails rspec-rails pundit

我在使用 Rspec 测试 Pundit 策略范围时遇到了障碍。基本上,我试图测试范围内的返回值是否严格限于用户。因此,我循环遍历为用户返回的所有问题,并确保 id 等于用户。

这是绿色的,但是当它是非公民角色时它会失败,因为 Scope 调用仅返回 Issue 对象。这不是什么大问题,因为每个角色都有一个 .where() 的范围,但这是一种代码味道,我可能做错了一些事情。

我有一个执行该操作的 IssueController

class IssuePolicy < ApplicationPolicy
  class Scope < Struct.new(:user, :scope)
    def resolve
      if user.role == 'citizen'
       scope.where(:user_id => user.id)
     else
       scope
     end
   end
end

为了在 rspec 中测试这一点,我必须这样做

require 'spec_helper'

describe IssuePolicy do

  before(:each) do
    @user = FactoryGirl.create(:user)
    @issue = FactoryGirl.create(:issue)
    @last_issue = FactoryGirl.create(:issue, user_id: User.last.id + 1)
  end

  context "for a citizen" do
    before(:each) do
      @user.update(role: 'citizen')
    end
    it "should only return their posts on index action" do
     @p = IssuePolicy::Scope.new(@user, Issue).resolve
     @p.each do |issue|
       expect{issue.user_id}.to eql(@user.id)
     end
   end
  end
end

最佳答案

好吧,我们进行了一些挖掘,发现了如何通过文档来测试这个问题,然后如何通过研究来确定范围。

irb> IssuePolicy::Scope.new(@user, Issue.all).resolve.class 

这将返回一个 ActiveRecord::QueryMethods::WhereChain

所以下一个问题实际上变成了我想测试它的结构或行为吗?我选择测试该行为是因为我真的只关心我的范围是否已正确应用。所以上面的范围测试对我来说很好。作为一个额外的帮助者,这是我如何测试只有员工角色可以访问而公民角色被拒绝的方法,希望它对某人有帮助。

需要“spec_helper”

describe IssuePolicy do
  subject { IssuePolicy }


  permissions :index?, :show?, :create?, :new?, :update?, :edit?, :destroy? do
    it "denies access to citizen" do
      expect(subject).not_to permit(FactoryGirl.create(:user, role: 'citizen'), Issue.create())
    end

    it "allows access to employee" do
      expect(subject).to permit(FactoryGirl.create(:user, role: 'employee'), Issue.create())
    end
  end


end

关于ruby-on-rails - Rails Pundit Rspec 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22885378/

相关文章:

ruby-on-rails - Rails 4 - Pundit with Rolify - 允许一组角色

ruby-on-rails - OmniAuth 和 openid : getting certain fields from openid provider

ruby-on-rails - 列出与 http 方法关联的所有 Rails 路线

ruby - 尽管未定义操作和路由,但 RSpec 成功

ruby-on-rails - 如何使用expect语法编写should_not be_valid

ruby-on-rails - Pundit:确保 current_user 是来自 params 的用户

ruby-on-rails - week_field 在序列化和反序列化后给出错误的周

ruby-on-rails - Websocket-rails 不适用于 Nginx 和 Unicorn 的生产环境

ruby-on-rails - 使用 get 和 delete 运行 Rspec 测试时获取错误数量的参数(2 个为 0)