Rails / Pundit / RSpec - #let或#subject在没有块的情况下调用

时间:2015-07-15 11:40:40

标签: ruby-on-rails rspec pundit

我已经用

创建了Pundit政策规范
rails g pundit:policy class

...但规格出现以下错误:

.rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.1/lib/rspec/core/memoized_helpers.rb:287:in `let': #let or #subject called without a block (RuntimeError)
from .rvm/gems/ruby-2.2.2/gems/rspec-core-3.3.1/lib/rspec/core/memoized_helpers.rb:405:in `subject'
from spec/policies/application_policy_spec.rb:10:in `block (2 levels) in <top (required)>'

但是在规范中,似乎主题是用块来调用的。这是生成的规范:

require 'rails_helper'

describe ApplicationPolicy do

  let(:user) { User.new }

  subject { described_class }

  permissions :index do
    expect(subject).to permit(user)
  end

  permissions :show? do
    pending "add some examples to (or delete) #{__FILE__}"
  end

  permissions :create? do
    pending "add some examples to (or delete) #{__FILE__}"
  end

  permissions :update? do
    pending "add some examples to (or delete) #{__FILE__}"
  end

  permissions :destroy? do
    pending "add some examples to (or delete) #{__FILE__}"
  end
end

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

我已经弄清楚了。我错过了?在索引操作中,必须在权限块内创建一个阻止,如:

  permissions :index? do
    it 'allows users to index' do  
      expect(subject).to permit(user)
    end
  end