简体   繁体   English

CanCan,如何使用无模式控制器测试rspec?

[英]CanCan, how to test an rspec with a modeless, controller?

I have an admin controller w/oa model 我有一个带OA模型的管理员控制器

class AdminController < ApplicationController

    # Sign In Required - Devise Check
    before_filter :authenticate_user!
    # CanCan
    before_filter :current_ability

  # CanCan for Non RESTful Controllers - https://github.com/ryanb/cancan/wiki/Non-RESTful-Controllers
  # We do this as there is no admin model/resouce
  authorize_resource :class => false

I'm unable to figure out how to write a spec for the controller. 我无法弄清楚如何为控制器编写规格。 I have the following: 我有以下几点:

  describe 'Admin User' do

    it "should be allow admin users to view admin#index" do
      @ability_for_user_4.can :read, Admin
      get :index
      assert_template :index
    end
  end

This fails with: uninitialized constant Admin 这将失败:未初始化的常量Admin

Any ideas? 有任何想法吗? Thanks 谢谢

Does your spec start with require 'spec_helper' ? 您的规格是否以require 'spec_helper' Most Ruby on Rails specs start with that and it will load the Rails environment into memory. 大多数Ruby on Rails规范都是以此为开头的,它将把Rails环境加载到内存中。

Otherwise, you need to create a mocked model so Rspec knows what an Admin is. 否则,您需要创建一个模拟模型,以便Rspec知道Admin是什么。

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

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