【发布时间】:2014-09-10 11:53:22
【问题描述】:
我正在安装一个具有 Pundit 授权的应用程序,当我尝试运行 RSpec 测试时,我得到:
undefined method `permissions'
for RSpec::ExampleGroups::UserPolicy:Class
(NoMethodError)
【问题讨论】:
标签: ruby-on-rails devise pundit
我正在安装一个具有 Pundit 授权的应用程序,当我尝试运行 RSpec 测试时,我得到:
undefined method `permissions'
for RSpec::ExampleGroups::UserPolicy:Class
(NoMethodError)
【问题讨论】:
标签: ruby-on-rails devise pundit
还记得把这个添加到你的 rails_helper
require 'pundit/rspec'
【讨论】:
parallel_tests 时,我的测试因此错误而失败。 rails_helper 上的 require 解决了我的问题。
找出问题所在...
rails_helper.rb 中的以下行已被注释掉:
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
激活它使测试正常工作:)
【讨论】:
我有 require 'pundit/rspec',但我在规范目录结构中拼错了“策略”:
spec/polices/my_policy_spec.rb
我的错误也返回了权限错误信息。
您可以通过正确拼写策略来修复它,或者将测试文件的类型设置为策略。
RSpec.describe MyPolicy, type: :policy do
...
...
end
【讨论】: