4

我正在安装具有 Pundit 授权的应用程序,当我尝试运行 RSpec 测试时,我得到:

undefined method `permissions'
  for RSpec::ExampleGroups::UserPolicy:Class
      (NoMethodError)
4

3 回答 3

14

还记得添加这个你的rails_helper

require 'pundit/rspec'
于 2015-03-09T11:35:19.747 回答
0

发现问题出在哪里...

以下行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 }

激活它使测试正常工作:)

于 2014-09-10T15:19:57.107 回答
0

require 'pundit/rspec',但我在规范目录结构中拼错了“策略”:

spec/polices/my_policy_spec.rb

我的错误还返回了权限错误消息。

您可以通过正确拼写策略来修复它,或者将测试文件的类型设置为策略。

RSpec.describe MyPolicy, type: :policy do
  ...
  ...
end
于 2021-08-15T21:15:39.473 回答