gpt4 book ai didi

ruby-on-rails - RSpec Devise sign_in 未在 Controller 中设置 current_user

转载 作者:行者123 更新时间:2023-12-05 08:10:05 24 4
gpt4 key购买 nike

我已按照以下说明进行操作: Devise wiki关于使用 RSpec 设置 Controller 测试,但我的测试失败了。

它没有呈现页面,而是重定向到 http://test.host

以下是响应中的一些内容:

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

我的测试 Controller 规范如下:

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(不知道它来自哪里)因为没有当前用户定义?

在我的应用程序 Controller 中,如果用户在其他地方登录,我有代码设置来注销用户。这似乎是用户被自动重定向的地方。从这里转储 current_user 我可以看到 'current_sign_in_ip' 是 nil,因为所有其他登录属性都没有设置。

应用程序 Controller

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

测试.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

最佳答案

因为你不需要描述两个描述,因为它们彼此独立(它们是两个不同的场景)这就是为什么 current_user 在第二个描述中为 nil

尝试定义一个用于创建用户并让他登录的方法,您可以通过在 support/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

然后删除第一个describe,第二个describe只写在get :index之前

login_user

另一种解决方案是像这样首先对 login_user 进行嵌套描述:

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

end

关于ruby-on-rails - RSpec Devise sign_in 未在 Controller 中设置 current_user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27287211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com