【问题标题】:How do I test this with rspec?我如何用 rspec 测试这个?
【发布时间】:2013-02-24 19:04:13
【问题描述】:

我正在使用 Sorcery gem 进行用户注册/登录。

此 gem 的一个功能是您要验证的任何控制器上的 require_login before_filter。

我在他们登录后为我的应用创建了一个dashboard 命名空间。例如/dashboard/reports/dashboard/employees 等。

路线文件:

# Dashboard                                                
namespace :dashboard do                                      
  # Recent Activity                                          
  get '' => redirect('/dashboard/recent-activity')           
  get 'recent-activity' => 'activities#index', :as => 'root' 
  # Other dashboard controllers and actions
end  

我将 before_filter 提取到它自己的控制器中:

“app/controllers/dashboard/base_controller.rb”

class Dashboard::BaseController < ApplicationController

  before_filter :require_login

end

我想做的是在某种测试中 100% 确定我在仪表板文件夹(或仪表板命名空间)中创建的任何新控制器都继承自 Dashboard::BaseController

例如我的活动控制器:

class Dashboard::ActivitiesController &lt; Dashboard::BaseController

我不想在几个月后去创建控制器,并且不小心让它继承自 ApplicationController,它仍然会但不会有登录功能。

我正在使用 RSpec

【问题讨论】:

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


    【解决方案1】:

    不敢相信自己的眼睛,我自己解决了这个问题......

    require 'spec_helper'
    
    describe Dashboard::BaseController do
    
      it "is the superclass of every dashboard namespaced controller" do
        Rails.application.eager_load!
        ApplicationController.descendants.each do |controller|
          if controller.to_s.include?("Dashboard::") && controller.to_s != "Dashboard::BaseController"
            expect(controller.superclass.to_s).to eq("Dashboard::BaseController")      
          end
        end
      end
    
    end    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多