简体   繁体   English

我如何用rspec测试呢?

[英]How do I test this with rspec?

I'm using the Sorcery gem for user signup/login. 我正在使用Sorcery gem进行用户注册/登录。

One feature of this gem is the require_login before_filter on any controller you want to authenticate. 该gem的一个功能是要验证的任何控制器上的require_login before_filter。

I have created a dashboard namespace for my app after they've logged in. For example /dashboard/reports or /dashboard/employees , etc. 他们登录后,我已经为我的应用程序创建了一个dashboard名称空间。例如/dashboard/reports/dashboard/employees等。

Routes file: 路线文件:

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

I extracted out the before_filter into it's own controller called: 我将before_filter提取到它自己的控制器中:

"app/controllers/dashboard/base_controller.rb" “应用程序/控制器/仪表板/base_controller.rb”

class Dashboard::BaseController < ApplicationController

  before_filter :require_login

end

What I'd like to do is make 100% sure in some kind of test that ANY new controller I create within the dashboard folder (or dashboard namespace), inherits from Dashboard::BaseController 我想做的是在某种测试中100%确保我在仪表板文件夹(或仪表板名称空间)中创建的任何新控制器都继承自Dashboard::BaseController

Such as my activities controller for example: 例如我的活动控制器:

class Dashboard::ActivitiesController < Dashboard::BaseController

I dont want to go creating controllers in a few months and accidentally make it inherit from ApplicationController which would still would but wouldnt have login functionality. 我不想在几个月内创建控制器,并且不小心使它继承自ApplicationController,而ApplicationController仍然会但不会具有登录功能。

I'm using RSpec 我正在使用RSpec

Can't quite believe my own eyes that I solved this on my own.... 不能完全相信自己的眼睛我自己解决了这个问题。

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    

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

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