2013-03-20 1 views
0

Я создаю базовый контроллер для раздела администратора проекта. Все контроллеры, находящиеся в разделе администрирования, наследуют его.Rspec + Devise + BaseController

#app/controllers/admins/base_controller.rb 

class Admins::BaseController < ApplicationController 
    layout "admin_cms" 
    before_filter :authenticate_admin! 
end 

-

#spec/controllers/admins/base_controller_spec.rb 

require 'spec_helper' 

describe Admins::BaseController do 
    controller do 
    def index 
    end 
    end 

    describe "before_filter#authenticate_admin!" do 
    before(:each) do 
     @admin = FactoryGirl.create(:admin) 
     @request.env["devise.mapping"] = Devise.mappings[:admin] 
    end 

    context "when admin is not logged in" do 
     it "redirect admin to sign_in path" do 
     get :index 
     response.should redirect_to new_admin_session_path 
     end 
    end 

    end 
end 

Я уже inclueded DEViSE :: TestHelpers на моем spec_helper.rb, и я получаю эту ошибку при запуске этой спецификации:

Admins::BaseController 
    before_filter#authenticate_admin! 
    when admin is not logged in 
     redirect admin to sign_in path (FAILED - 1) 

Failures: 

    1) Admins::BaseController before_filter#authenticate_admin! when admin is not logged  in redirect admin to sign_in path 
    Failure/Error: get :index 
    ActionView::MissingTemplate: 
     Missing template anonymous/index, application/index with {:locale=>[:en],  :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: 
     * "#<RSpec::Rails::ViewRendering::EmptyTemplatePathSetDecorator:0xbaf75d4>" 
    # ./spec/controllers/admins/base_controller_spec.rb:17:in `block (4 levels) in <top (required)>' 

Finished in 0.17124 seconds 
1 example, 1 failure 

Failed examples: 

rspec ./spec/controllers/admins/base_controller_spec.rb:16 # Admins::BaseController before_filter#authenticate_admin! when admin is not logged in redirect admin to sign_in path 

Я изменил моя спецификация к этому:

require 'spec_helper' 

describe Admins::BaseController do 
    controller do 
    def index 
     render nothing: true 
    end 
    end 

    describe "before_filter#authenticate_admin!" do 
    context "when admin is not logged in" do 
     it "redirect admin to sign_in path" do 
     get :index 
     response.should redirect_to new_admin_session_path 
     end 
    end 

    end 
end 

и теперь я получаю эту ошибку:

Failures: 

    1) Admins::BaseController before_filter#authenticate_admin! when admin is not logged in redirect admin to sign_in path 
    Failure/Error: response.should redirect_to new_admin_session_path 
     Expected response to be a <:redirect>, but was <200> 

Так что, по какой-то причине это не входит в authenticate_admin! перед фильтром. Я потерян. Еще раз спасибо.

Я использую Rails 3.2.13, Ruby 2.0.0, Rspec-rails 2.13.0 и Devise 2.2.3. Я действительно придираюсь, если кто-то может помочь мне в этом. Заранее спасибо.

ответ

3

Ну, через 3 часа я обнаружил, что проблема заключается в определении анонимного контроллера.

вместо:

controller do 
    def index 
    end 
end 

Я использовал:

controller(Admins::Base) do 
    def index 
    end 
end 

Вы должны всегда указывать анонимный контроллер, который вы тестируете, если не ApplicationController тот, который вы пытаетесь проверить.