【问题标题】:Testing a databaseless Rails 5 application with rspec-rails使用 rspec-rails 测试无数据库 Rails 5 应用程序
【发布时间】:2023-03-17 23:33:02
【问题描述】:

我有一个不受任何数据库支持的 Rails 5.0.1 应用程序。在尝试编写控制器测试时,我得到了一个ActiveRecord::ConnectionNotEstablished 异常,即使我已明确配置我的spec/rails_helper.rb 不使用ActiveRecord 并删除了config/application.rb 中对active_record 的引用。 RSpec 正在尝试在需要建立数据库连接的事务中运行此类测试。我附上了堆栈跟踪以供参考,我使用这个 Github 问题来设置没有ActiveRecordrspec-rails for a rails project without db connection 的 RSpec。

失败:

1) ApplicationController 真假不等于真 失败/错误:引发 ConnectionNotEstablished,“找不到 ID 为 #{spec_name} 的连接池。”除非池

 ActiveRecord::ConnectionNotEstablished:
   No connection pool with id primary found.
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:882:in `retrieve_connection'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_handling.rb:128:in `retrieve_connection'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_handling.rb:91:in `connection'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/fixtures.rb:516:in `create_fixtures'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/fixtures.rb:1015:in `load_fixtures'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/fixtures.rb:988:in `setup_fixtures'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/activerecord-5.0.1/lib/active_record/fixtures.rb:852:in `before_setup'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-rails-3.6.1/lib/rspec/rails/adapters.rb:126:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:447:in `instance_exec'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:375:in `execute_with'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:606:in `block (2 levels) in run_around_example_hooks_for'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:342:in `call'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:607:in `run_around_example_hooks_for'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/hooks.rb:464:in `run'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:457:in `with_around_example_hooks'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:500:in `with_around_and_singleton_context_hooks'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example.rb:251:in `run'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:627:in `block in run_examples'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:623:in `map'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:623:in `run_examples'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:589:in `run'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:590:in `block in run'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:590:in `map'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/example_group.rb:590:in `run'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `block (3 levels) in run_specs'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `map'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:118:in `block (2 levels) in run_specs'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/configuration.rb:1894:in `with_suite_hooks'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:113:in `block in run_specs'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/reporter.rb:79:in `report'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:112:in `run_specs'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:87:in `run'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:71:in `run'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/lib/rspec/core/runner.rb:45:in `invoke'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/gems/rspec-core-3.6.0/exe/rspec:4:in `<top (required)>'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/bin/rspec:23:in `load'
 # /home/vagrant/.bundle/easypost_admin/ruby/2.3.0/bin/rspec:23:in `<top (required)>'
 # /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `load'
 # /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `kernel_load'
 # /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:24:in `run'
 # /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:304:in `exec'
 # /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
 # /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
 # /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
 # /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
 # /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:11:in `start'
 # /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/exe/bundle:27:in `block in <top (required)>'
 # /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/friendly_errors.rb:98:in `with_friendly_errors'
 # /opt/ruby2.3/lib64/ruby/gems/2.3.0/gems/bundler-1.12.5/exe/bundle:19:in `<top (required)>'
 # /vagrant/easypost_admin/vendor/bundle/bundle:23:in `load'
 # /vagrant/easypost_admin/vendor/bundle/bundle:23:in `<main>'
 #
 #   Showing full backtrace because every line was filtered out.
 #   See docs for RSpec::Configuration#backtrace_exclusion_patterns and
 #   RSpec::Configuration#backtrace_inclusion_patterns for more information.

在 0.0058 秒内完成(文件加载需要 2.29 秒) 1 个示例,1 个失败

失败的例子:

rspec ./spec/controllers/application_controller_spec.rb:15 # ApplicationController 真假不等于真

更新

rails_help.rb

require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
# ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  # config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = false
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
end

spec_helper.rb

RSpec.configure do |config|
  # rspec-expectations config goes here. You can use an alternate
  # assertion/expectation library such as wrong or the stdlib/minitest
  # assertions if you prefer.
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  # rspec-mocks config goes here. You can use an alternate test double
  # library (such as bogus or mocha) by changing the `mock_with` option here.
  config.mock_with :rspec do |mocks|
    # Prevents you from mocking or stubbing a method that does not exist on
    # a real object. This is generally recommended, and will default to
    # `true` in RSpec 4.
    mocks.verify_partial_doubles = true
  end

  config.shared_context_metadata_behavior = :apply_to_host_groups
end

【问题讨论】:

  • 你能给我们看看你的 spec_helper.rbrails_helper.rb 吗?
  • 您是否将rails_helper.rb 中的config.use_transactional_fixtures = true 更改为false?
  • @Leito 我更新了我的问题以包括rails_helper.rbspec_helper.rb。 @mindlis 是 config.use_transactional_fixtures = false 它设置为 false。

标签: ruby-on-rails rails-activerecord rspec-rails ruby-on-rails-5 no-database


【解决方案1】:

我在没有数据库的 rails 5.2.0.alpha 应用程序中遇到了同样的问题 配置。所以我查看了 rspec-rails 代码,并发现了这一点 导致所有麻烦的行:

rspec-rails/lib/rspec/rails/fixture_support.rb:

if defined?(ActiveRecord::TestFixtures)

它检查加载的 ActiveRecord 常量,该常量实际上已加载。一世 也有require 'active_record/railtie' 被注释掉了 config/application.rb。然后我弄清楚谁需要 ActiveStorage gem (捆绑锁)。就我而言,它是 ActiveStorage。

我只使用没有数据库的 ActiveStorage 功能,所以我不得不 删除我的 spec/rails_helper.rb 上的 ActiveRecord 常量:

# frozen_string_literal: true

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)

# Remove the ActiveRecord constant, because it is autloaded by
# ActiveStorage and not needed for our application. The presence
# of the ActiveRecord constant causes rspec-rails to include
# extra fixture support, which results in:
#
#   ActiveRecord::ConnectionNotEstablished:
#     No connection pool with 'primary' found.
#
Object.send(:remove_const, :ActiveRecord)

注意顺序,需要放在后面

require File.expand_path('../../config/environment', __FILE__)

之前

require 'rspec/rails'

这个方案只是修改了测试环境。

更新

我创建了an issue on rspec-rails,它的目标是创建全局 rspec 配置以关闭 ActiveRecord 处理。

【讨论】:

  • 感谢您的破解解决方案!我不会那么容易解决的。很遗憾,您对 rspec-rails 的问题没有引起他们的注意来解决问题。对于那些有同样问题的人,我使用的是 Rails 5.2 和 Rspec-rails 3.7。
【解决方案2】:

fix for this 刚刚合并到 rspec-rails,据说下周有时会与 RSpec Rails 4.0 一起发布。

您必须在spec/rails_helper.rb 中设置以下内容:

config.use_active_record = false

同样,问题是如果您没有配置/使用数据库连接,但 ActiveRecord 仍然存在(由于 Bundler 加载它或其他一些原因)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多