【问题标题】:Rspec spec failing -- undefined method `permissions' for RSpec::ExampleGroups::UserPolicy:Class (NoMethodError)Rspec 规范失败 - RSpec::ExampleGroups::UserPolicy:Class (NoMethodError) 的未定义方法“权限”
【发布时间】:2014-09-10 11:53:22
【问题描述】:

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

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

【问题讨论】:

    标签: ruby-on-rails devise pundit


    【解决方案1】:

    还记得把这个添加到你的 rails_helper

    require 'pundit/rspec'
    

    【讨论】:

    • 当我开始使用 parallel_tests 时,我的测试因此错误而失败。 rails_helper 上的 require 解决了我的问题。
    【解决方案2】:

    找出问题所在...

    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 }
    

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

    【讨论】:

      【解决方案3】:

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

      spec/polices/my_policy_spec.rb

      我的错误也返回了权限错误信息。

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

      RSpec.describe MyPolicy, type: :policy do
        ...
        ...
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-18
        • 1970-01-01
        • 1970-01-01
        • 2014-09-26
        相关资源
        最近更新 更多