如何为无模式控制器编写CanCan rspec?

时间:2013-02-24 02:28:29

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

我有一个模型group.rb,然后是一个无模式的控制器group_invitations.rb。

GroupInvitationsController

  before_filter :find_group_by_group_id
  before_filter :authenticate_user!
  before_filter :current_ability
  authorize_resource :class => false

  def current_ability
    @current_ability ||= Ability.new(current_user, @group)
  end

当我为此写一个rspec时:

  it "should be able to create" do
    ability = Ability.new(nil)
    ability.should be_able_to(:create, GroupInvitation.new)
  end

rspec然后错误:

NameError:        未初始化的常数GroupInvitation

如何设置rspec来测试这个无模式控制器?感谢

1 个答案:

答案 0 :(得分:1)

您需要致电@ability.should be_able_to(:create, :group_invitation)。您可以在documentation中了解使用无模型控制器时的授权内容。

这是相关部分:

class ToolsController < ApplicationController
  authorize_resource :class => false
  def show
    # automatically calls authorize!(:show, :tool)
  end
end