如何测试Pundit ApplicationPolicy?

时间:2019-05-17 22:46:55

标签: ruby-on-rails rspec rspec-rails pundit

这是我第一次使用Pundit,但我并不真正熟悉它。 我想测试我的应用程序策略,以确保所有方法都引发NotImplementError,因为我想确保所有方法都在子类策略中实现。

我尝试模拟一个子类,但不确定自己在做什么,将不胜感激。

# frozen_string_literal: true

# This class wrap the application policy
# and raise NotImplementedError if a method is not implemented
class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = user
    @record = record
  end

  def index?
    raise NotImplementedError
  end

  def show?
    raise NotImplementedError
  end

  def create?
    raise NotImplementedError
  end

  def new?
    raise NotImplementedError
  end

  def update?
    raise NotImplementedError
  end

  def edit?
    raise NotImplementedError
  end

  def destroy?
    raise NotImplementedError
  end

  # Scopes are used to define view listing records
  # which a particular user has access to.
  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      scope.all
    end
  end
end

我希望子类在使用Rpsec定期测试时会引发NotImplementedError。

任何帮助/建议将不胜感激!

0 个答案:

没有答案