設定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
您的班級的名稱是管理員或用戶?因爲如果它是管理員而不是用戶,在你的控制器上你需要放一個'before_filter authenticate_administrator!'而不是'authenticate_user!' – cefigueiredo 2014-12-04 19:34:34
沒有。管理員只是將admin角色添加到用戶的工廠助手。 – Rumpleteaser 2014-12-04 23:33:10