简体   繁体   English

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

[英]How to write a CanCan rspec for a modeless controller?

I have a model group.rb and then a controller group_invitations.rb which is modeless. 我有一个模型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

When I write a rspec for this: 当我为此编写一个rspec时:

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

rspec then errors with: rspec然后出现以下错误:

NameError: uninitialized constant GroupInvitation NameError:未初始化的常量GroupInvitation

How do I setup rspec to test this modeless controller? 如何设置rspec来测试此无模式控制器? Thanks 谢谢

You need to call @ability.should be_able_to(:create, :group_invitation) . 您需要调用@ability.should be_able_to(:create, :group_invitation) You can read about what is authorized when using a model-less controller in the documentation . 您可以在文档中阅读有关使用无模型控制器的授权内容

This is the relevant section: 这是相关的部分:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM