我正在安装具有 Pundit 授权的应用程序,当我尝试运行 RSpec 测试时,我得到:
undefined method `permissions'
for RSpec::ExampleGroups::UserPolicy:Class
(NoMethodError)
我正在安装具有 Pundit 授权的应用程序,当我尝试运行 RSpec 测试时,我得到:
undefined method `permissions'
for RSpec::ExampleGroups::UserPolicy:Class
(NoMethodError)
还记得添加这个你的rails_helper
require 'pundit/rspec'
发现问题出在哪里...
以下行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