Rails 6升级至Rails 7后测试报错:each_value方法未定义
Rails 7测试报错:undefined method `each_value' for nil:NilClass
问题描述
将Rails 6项目升级至Rails 7以集成Tailwind后,项目运行正常,但执行任何测试都会报错。移除所有测试可正常通过,添加最简测试代码后执行rails t仍触发错误。
最简测试代码(post_test.rb)
require "test_helper" class PostTest < ActiveSupport::TestCase test "the truth" do assert true end end
报错详情
rails t Running 1 tests in a single process (parallelization threshold is 50) Run options: --seed 58982 # Running: E Error: PostTest#test_the_truth: NoMethodError: undefined method `each_value' for nil:NilClass @stubs.each_value do |object_stubs| ^^^^^^^^^^^ /home/sam/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.3.1/lib/active_support/testing/time_helpers.rb:39:in `unstub_all!' /home/sam/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.3.1/lib/active_support/testing/time_helpers.rb:211:in `travel_back' /home/sam/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.3.1/lib/active_support/testing/time_helpers.rb:71:in `after_teardown' /home/sam/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.3.1/lib/active_support/current_attributes/test_helper.rb:10:in `after_teardown' /home/sam/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.3.1/lib/active_support/execution_context/test_helper.rb:10:in `after_teardown' /home/sam/.rvm/gems/ruby-3.1.0/gems/activerecord-7.0.3.1/lib/active_record/test_fixtures.rb:15:in `after_teardown' /home/sam/.rvm/gems/ruby-3.1.0/gems/activesupport-7.0.3.1/lib/active_support/testing/setup_and_teardown.rb:51:in `after_teardown' rails test test/models/post_test.rb:7 Finished in 0.036355s, 27.5065 runs/s, 27.5065 assertions/s. 1 runs, 1 assertions, 0 failures, 1 errors, 0 skips
补充配置文件
test_helper.rb
require_relative "../config/environment" require "rails/test_help" class ActiveSupport::TestCase # Run tests in parallel with specified workers parallelize(workers: :number_of_processors) # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all # Add more helper methods to be used by all tests here... end
config/environments/test.rb
require "active_support/core_ext/integer/time" # The test environment is used exclusively to run your application's # test suite. You never need to work with it otherwise. Remember that # your test database is "scratch space" for the test suite and is wiped # and recreated between test runs. Don't rely on the data there! Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. # Turn false under Spring and add config.action_view.cache_template_loading = true. config.cache_classes = true # Eager loading loads your whole application. When running a single test locally, # this probably isn't necessary. It's a good idea to do in a continuous integration # system, or in some way before deploying your code. config.eager_load = ENV["CI"].present? # Configure public file server for tests with Cache-Control for performance. config.public_file_server.enabled = true config.public_file_server.headers = { "Cache-Control" => "public, max-age=#{1.hour.to_i}" } # Show full error reports and disable caching. config.consider_all_requests_local = true config.action_controller.perform_caching = false config.cache_store = :null_store # Raise exceptions instead of rendering exception templates. config.action_dispatch.show_exceptions = false # Disable request forgery protection in test environment. config.action_controller.allow_forgery_protection = false # Store uploaded files on the local file system in a temporary directory. config.active_storage.service = :test config.action_mailer.perform_caching = false # Tell Action Mailer not to deliver emails to the real world. # The :test delivery method accumulates sent emails in the # ActionMailer::Base.deliveries array. config.action_mailer.delivery_method = :test # Print deprecation notices to the stderr. config.active_support.deprecation = :stderr # Raise exceptions for disallowed deprecations. config.active_support.disallowed_deprecation = :raise # Tell Active Support which deprecation messages to disallow. config.active_support.disallowed_deprecation_warnings = [] # Raises error for missing translations. # config.i18n.raise_on_missing_translations = true # Annotate rendered view with file names. # config.action_view.annotate_rendered_view_with_filenames = true end
解决办法
1. 禁用并行测试
在test_helper.rb中注释掉并行测试配置:
# parallelize(workers: :number_of_processors)
并行测试可能导致测试间状态污染,尤其是时间相关的stub未被正确清理。
2. 手动初始化TimeHelpers的stubs
在ActiveSupport::TestCase中添加setup钩子初始化变量:
class ActiveSupport::TestCase # ... 现有代码 ... setup do @stubs ||= {} end end
确保@stubs在测试执行前被初始化,避免调用each_value时对象为nil。
3. 升级Rails版本
该bug已在Rails 7.0.4及后续版本中修复。执行以下命令升级到最新7.0.x版本:
bundle update rails
内容的提问来源于stack exchange,提问作者user7203397
相关产品推荐
相关产品推荐

