【问题标题】:RSpec and Cancancan: all negative tests failingRSpec 和 Cancancan:所有阴性测试均失败
【发布时间】:2015-02-04 21:31:01
【问题描述】:

谁能帮我理解为什么只有我的阴性(即should_not)测试会失败?我所有的should 测试都通过了,所以这让我觉得我有一些配置错误,虽然我不知道。

ability.rb

# encoding: utf-8
#
class Ability
  include CanCan::Ability

  def initialize(user)
    @user = user || User.new
    if @user.administrator?
      administrator_abilities
    elsif @user.client?
      client_abilities
    elsif @user.guest?
      guest_abilities
    end
    can :read, :share
  end

  private

  def administrator_abilities
    can :manage, :all
    can [:index, :filter, :new, :create], :administration
  end

  def client_abilities
    cannot :manage, administrator_resources
    can :manage, [:account, :provider_auth, :user_auth]
    can :read, [Component, Cover, Introduction, Template]
    can :manage, client_made_resources, user_id: @user.id
  end

  def guest_abilities
    cannot :manage, client_made_resources
    cannot :manage, administrator_resources
    can :create, Expression
  end

  def client_made_resources
    [Authorisation, Document, Component,
     CustomArticle, EditedArticle, Invoice, Order, Photo]
  end

  def administrator_resources
    [Brand, Chart, Component, Cover, Expression, Introduction, Mode,
     Preference, Price, Template, User, UserNote]
  end
end

user_spec.rb

# encoding: utf-8
#
require 'spec_helper'
require 'cancan/matchers'
def client_made_resources
  [Authorisation, Document, Component, CustomArticle, EditedArticle, Invoice,
   Order, Photo]
end

def administrator_resources
  [Brand, Chart, Component, Cover, Expression, Introduction, Mode, Preference,
   Price, Template, User, UserNote]
end

RSpec.describe Ability do
  let(:user) { create(:user, state: 'guest') }
  subject    { Ability.new(user) }

  context 'guest' do
    client_made_resources.each do |r|
      it "should not be able to manage client's #{r}" do
        expect(subject).to_not be_able_to(:manage, r.new)
      end
    end

每个阴性测试都有这个结果:

rspec ./spec/models/user_spec.rb:69 # Ability guest should not be able to manage client's Document

感谢您的帮助。

【问题讨论】:

  • 对不起,我没有给你答案。不过有一些提示:也许您应该将所有不能移动到首先运行的单个块中?如果用户通过所有角色检查,这将减少重复并更安全。同样,一旦您说管理员“可以:管理,:所有”,就不需要额外的定义。 :manage 涵盖所有操作, :all 涵盖所有对象。
  • 另外,如果你让资源列表成为类常量或类方法,你可以在测试中直接引用它们,而不必担心在两个文件之间保持列表同步。跨度>
  • 至于报错,你验证过user.administrator吗?和user.client?对于您的工厂对象实际上是错误的吗?我们需要查看模型和工厂才能完全了解发生了什么。
  • 感谢您的建议,我已经制作了一个具有相关权限方法的模块。默认情况下,工厂是client,所以如果是这种情况,至少客户端测试应该通过。真的卡住了。

标签: ruby-on-rails ruby-on-rails-3 rspec cancan cancancan


【解决方案1】:

我不知道为什么,但我已经重构了我的测试,它们现在可以按预期工作。也许这会对某人有所帮助。

RSpec.describe User do
  describe 'Abilities' do
    context 'guest' do
      let(:user) { create(:user, state: 'guest') }
      (client_made_resources + administrator_resources).each do |r|
        it "cannot manage #{r}" do
          ability = Ability.new(user)
          assert ability.cannot?(:manage, r.new)
        end
      end

      it 'can read shares' do
        ability = Ability.new(user)
        assert ability.can?(:read, :share)
      end
...

【讨论】:

  • 看起来问题可能出在 be_able_to 匹配器上。如果您可以在一个简单的应用程序中复制它,请提交错误报告。不过,ryanb 的 cancan 回购似乎不再维护。您可能想查看这个 fork 以获得更新的版本:github.com/cancancommunity/cancancan
猜你喜欢
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多