2014-12-04 78 views
1

設定CURRENT_USER我按照中的說明: Devise wiki上設置控制器測試使用RSpec,但我的測試失敗。RSpec的設計sign_in不是在控制器

反而使其出現在頁面中被重定向到http://test.host

下面是從響應的幾件事情:

@current_user=nil 
@_response_body=["<html><body>You are being <a href=\"http://test.host/\">redirected</a>.</body></html>"] 

我的測試控制器規格的樣子:

require 'rails_helper' require 'devise' 

RSpec.describe Services::MyController, :type => :controller do 
    include Devise::TestHelpers 
    include Warden::Test::Helpers 

    describe "administrator" do 
    before :each do 
     @request.env["devise.mapping"] = Devise.mappings[:user] 
     #administrator wraps user and adds role 
     @admin = FactoryGirl.create(:administrator) 
     @admin.confirm! 
     sign_in @admin 
    end 
    describe "GET index" do 
     it "returns http success" do 
     #puts current_user.to_yaml 
     get :index 
     expect(response).to be_success 
     end 
    end 
    end 
end 

它看起來像自動重定向到test.host(不知道從哪裏來),因爲沒有當前的用戶定義?

在我的應用程序控制器中,我有代碼設置,以便在用戶登錄到其他地方登錄時將其註銷。這似乎是用戶被自動重定向的地方。從轉儲current_user在這裏我可以看到'current_sign_in_ip'是零,因爲所有其他登錄屬性沒有設置。

ApplicationController中

def check_request_ip 
    puts current_user.to_yaml 
    puts request.remote_ip 
    unless current_user.nil? || request.remote_ip == current_user.current_sign_in_ip 
     Rails.logger.warn "User already logged in from #{current_user.current_sign_in_ip} and trying to log in from #{request.remote_ip}." 
     sign_out_and_redirect(current_user) 
    end 
    end 

rails_helper.rb

ENV["RAILS_ENV"] ||= 'test' 
require 'spec_helper' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } 

ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) 

RSpec.configure do |config| 

    config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    config.use_transactional_fixtures = true 

    config.infer_spec_type_from_file_location! 
end 

spec_helper.rb

require 'factory_girl' 
require 'devise' 
RSpec.configure do |config| 
    begin 
    config.include Warden::Test::Helpers 
    config.include Devise::TestHelpers, :type => :controller 
    config.include FactoryGirl::Syntax::Methods 

    config.filter_run :focus 
    config.run_all_when_everything_filtered = true 

    if config.files_to_run.one? 
     config.default_formatter = 'doc' 
    end 
    config.profile_examples = 10 

    config.order = :random 

    Kernel.srand config.seed 

    config.expect_with :rspec do |expectations| 
     expectations.syntax = [:expect, :should] 
    end 

    config.mock_with :rspec do |mocks| 
     mocks.syntax = :expect 
     mocks.verify_partial_doubles = true 
    end 
    end 
end 

test.rb

Go2Asset::Application.configure do 

    config.stretches = Rails.env.test? ? 1 : 10 
    config.cache_classes = true 

    config.eager_load = false 

    config.serve_static_assets = true 
    config.static_cache_control = "public, max-age=3600" 

    config.consider_all_requests_local = true 
    config.action_controller.perform_caching = false 

    config.action_dispatch.show_exceptions = false 

    config.action_controller.allow_forgery_protection = false 

    config.action_mailer.delivery_method = :test 
    config.action_mailer.default_url_options = { :host => 'localhost:3000' } 

    config.active_support.deprecation = :stderr 
end 
+0

您的班級的名稱是管理員或用戶?因爲如果它是管理員而不是用戶,在你的控制器上你需要放一個'before_filter authenticate_administrator!'而不是'authenticate_user!' – cefigueiredo 2014-12-04 19:34:34

+0

沒有。管理員只是將admin角色添加到用戶的工廠助手。 – Rumpleteaser 2014-12-04 23:33:10

回答

-1

由於您不需要描述兩個描述,因爲它們彼此獨立(它們是兩個不同的場景),這就是爲什麼current_user在第二個描述中爲零

嘗試定義用於創建用戶的方法並簽下他的,你可以通過創建支持的方法/這麼做devise.rb如下:

module ControllerMacros 
    def login_user 
    before(:each) do 
     @request.env["devise.mapping"] = Devise.mappings[:user] 
     user = FactoryGirl.create(:user) # in factories.rb you should create a factory for user 
     user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the "confirmable" module 
     sign_in user 
    end 
    end 
end 

RSpec.configure do |config| 
    config.include Devise::TestHelpers, :type => :controller 
    config.extend ControllerMacros, :type => :controller 
end 

然後刪除首先介紹,在第二描述了只寫之前得到:指數

login_user 

另一個解決方案是hav嵌套描述到login_user像這樣:

describe "something" do 
    login_user 
    describe "something 1" do 
    #yourcode 
    end 

end 
+0

我的描述是嵌套的。我希望代碼在每個嵌套描述之前運行以設置環境。在任何情況下,我嘗試移動描述內的代碼,但它仍然無法正常工作。 – Rumpleteaser 2014-12-04 13:49:33

+0

即使移動單個測試用例中的代碼也會導致相同的問題。即使我試圖在登錄後放置當前用戶,我也什麼也得不到。 – Rumpleteaser 2014-12-04 13:58:12

+0

我想我可能會知道原因,可否請粘貼您的rails_helper.rb和env.rb? – 2014-12-04 17:25:31