2013-03-09 26 views
0

我使用魔法寶石進行用戶註冊/登錄。如何用rspec測試這個?

此寶石的一個功能是您要驗證的任何控制器上的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 < Dashboard::BaseController

我不想去幾個月創建控制器和小心讓它從ApplicationController中繼承其仍將會,但不會必須登錄功能。

我使用的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