From e3db248753cf6b7deb562a3b6db549b64806c523 Mon Sep 17 00:00:00 2001 From: Shane Cavanaugh Date: Wed, 9 Mar 2022 17:07:38 -0700 Subject: [PATCH 1/6] Include ActiveSupport::Testing::TaggedLogging for Rails >= 7 --- lib/rspec/rails/example/rails_example_group.rb | 5 +++++ spec/rspec/rails/example/rails_example_group_spec.rb | 10 ++++++++++ 2 files changed, 15 insertions(+) create mode 100644 spec/rspec/rails/example/rails_example_group_spec.rb diff --git a/lib/rspec/rails/example/rails_example_group.rb b/lib/rspec/rails/example/rails_example_group.rb index 3a3ecbe704..2a09730bc6 100644 --- a/lib/rspec/rails/example/rails_example_group.rb +++ b/lib/rspec/rails/example/rails_example_group.rb @@ -12,6 +12,11 @@ module RailsExampleGroup include RSpec::Rails::MinitestLifecycleAdapter include RSpec::Rails::MinitestAssertionAdapter include RSpec::Rails::FixtureSupport + + if ::Rails::VERSION::MAJOR >= 7 + require 'active_support/testing/tagged_logging' + include ActiveSupport::Testing::TaggedLogging + end end end end diff --git a/spec/rspec/rails/example/rails_example_group_spec.rb b/spec/rspec/rails/example/rails_example_group_spec.rb new file mode 100644 index 0000000000..b09c46606b --- /dev/null +++ b/spec/rspec/rails/example/rails_example_group_spec.rb @@ -0,0 +1,10 @@ +module RSpec::Rails + RSpec.describe RailsExampleGroup do + if ::Rails::VERSION::MAJOR >= 7 + it 'includes ActiveSupport::Testing::TaggedLogging' do + expect(described_class.include?(::ActiveSupport::Testing::TaggedLogging)).to eq(true) + expect(described_class.private_instance_methods).to include(:tagged_logger) + end + end + end +end From 1c4d459c4de9702bafc48428ccac1c4f21dacd22 Mon Sep 17 00:00:00 2001 From: Shane Cavanaugh Date: Thu, 10 Mar 2022 10:34:12 -0700 Subject: [PATCH 2/6] Snippet --- ...ude_activesupport_testing_tagged_logger.rb | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 snippets/include_activesupport_testing_tagged_logger.rb diff --git a/snippets/include_activesupport_testing_tagged_logger.rb b/snippets/include_activesupport_testing_tagged_logger.rb new file mode 100644 index 0000000000..f77a7f36ea --- /dev/null +++ b/snippets/include_activesupport_testing_tagged_logger.rb @@ -0,0 +1,75 @@ +if __FILE__ =~ /^snippets/ + fail "Snippets are supposed to be run from their own directory to avoid side " \ + "effects as e.g. the root `Gemfile`, or `spec/spec_helpers.rb` to be " \ + "loaded by the root `.rspec`." +end + +# We opt-out from using RubyGems, but `bundler/inline` requires it +require 'rubygems' + +require "bundler/inline" + +# We pass `false` to `gemfile` to skip the installation of gems, +# because it may install versions that would conflict with versions +# from the main `Gemfile.lock`. +gemfile(false) do + source "https://rubygems.org" + + git_source(:github) { |repo| "https://github.com/#{repo}.git" } + + # Those Gemfiles carefully pick the right versions depending on + # settings in the ENV, `.rails-version` and `maintenance-branch`. + Dir.chdir('..') do + eval_gemfile 'Gemfile-sqlite-dependencies' + # This Gemfile expects `maintenance-branch` file to be present + # in the current directory. + eval_gemfile 'Gemfile-rspec-dependencies' + # This Gemfile expects `.rails-version` file + eval_gemfile 'Gemfile-rails-dependencies' + end + + gem "rspec-rails", path: "../" +end + +# Run specs at exit +require "rspec/autorun" + +require "rails" +require "active_record/railtie" +require "active_job/railtie" +require "rspec/rails" + +ActiveJob::Base.queue_adapter = :test + +# This connection will do for database-independent bug reports +ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") + +RSpec.configure do |config| + config.use_transactional_fixtures = true +end + +class TestJob < ActiveJob::Base + def perform; end +end + +class TestError < StandardError; end + +RSpec.describe 'Foo', type: :job do + include ::ActiveJob::TestHelper + + describe 'error raised in perform_enqueued_jobs with block' do + it 'raises the explicitly thrown error' do + allow_any_instance_of(TestJob).to receive(:perform).and_raise(TestError) + + # Rails 7+ wraps unexpected errors in tests + expected_error = if Rails::VERSION::MAJOR >= 7 + Minitest::UnexpectedError.new(TestError) + else + TestError + end + + expect { perform_enqueued_jobs { TestJob.perform_later } } + .to raise_error(expected_error) + end + end +end From cd1558d7f059982473683a54d605fc87c6c09c07 Mon Sep 17 00:00:00 2001 From: Shane Cavanaugh Date: Fri, 11 Mar 2022 09:56:01 -0700 Subject: [PATCH 3/6] Remove unnecessary config --- snippets/include_activesupport_testing_tagged_logger.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/snippets/include_activesupport_testing_tagged_logger.rb b/snippets/include_activesupport_testing_tagged_logger.rb index f77a7f36ea..e1c48edfef 100644 --- a/snippets/include_activesupport_testing_tagged_logger.rb +++ b/snippets/include_activesupport_testing_tagged_logger.rb @@ -44,10 +44,6 @@ # This connection will do for database-independent bug reports ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") -RSpec.configure do |config| - config.use_transactional_fixtures = true -end - class TestJob < ActiveJob::Base def perform; end end From 8201c7e3e943490f5d10d425258e6591abc79aa8 Mon Sep 17 00:00:00 2001 From: Shane Cavanaugh Date: Fri, 11 Mar 2022 09:56:23 -0700 Subject: [PATCH 4/6] Handle specific error for Rails 6.1+ --- snippets/include_activesupport_testing_tagged_logger.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/include_activesupport_testing_tagged_logger.rb b/snippets/include_activesupport_testing_tagged_logger.rb index e1c48edfef..dfd99ebea9 100644 --- a/snippets/include_activesupport_testing_tagged_logger.rb +++ b/snippets/include_activesupport_testing_tagged_logger.rb @@ -57,8 +57,8 @@ class TestError < StandardError; end it 'raises the explicitly thrown error' do allow_any_instance_of(TestJob).to receive(:perform).and_raise(TestError) - # Rails 7+ wraps unexpected errors in tests - expected_error = if Rails::VERSION::MAJOR >= 7 + # Rails 6.1+ wraps unexpected errors in tests + expected_error = if Rails::VERSION::STRING.to_f >= 6.1 Minitest::UnexpectedError.new(TestError) else TestError From 8e03e130afef1fb14a10deaccaabef0436a1eec4 Mon Sep 17 00:00:00 2001 From: Shane Cavanaugh Date: Fri, 11 Mar 2022 10:24:38 -0700 Subject: [PATCH 5/6] Maybe this will fix the jRuby error --- snippets/include_activesupport_testing_tagged_logger.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/snippets/include_activesupport_testing_tagged_logger.rb b/snippets/include_activesupport_testing_tagged_logger.rb index dfd99ebea9..e349818a3b 100644 --- a/snippets/include_activesupport_testing_tagged_logger.rb +++ b/snippets/include_activesupport_testing_tagged_logger.rb @@ -57,11 +57,12 @@ class TestError < StandardError; end it 'raises the explicitly thrown error' do allow_any_instance_of(TestJob).to receive(:perform).and_raise(TestError) + test_error = TestError.new('foo') # Rails 6.1+ wraps unexpected errors in tests expected_error = if Rails::VERSION::STRING.to_f >= 6.1 - Minitest::UnexpectedError.new(TestError) + Minitest::UnexpectedError.new(test_error) else - TestError + test_error end expect { perform_enqueued_jobs { TestJob.perform_later } } From e4e42b8e19193228492dfb5332946dc2223f29ee Mon Sep 17 00:00:00 2001 From: Shane Cavanaugh Date: Fri, 11 Mar 2022 11:03:47 -0700 Subject: [PATCH 6/6] Revert "Maybe this will fix the jRuby error" This reverts commit 5d9d3b0d7edbd627510ebb0d0de9b035fd024238. --- snippets/include_activesupport_testing_tagged_logger.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/snippets/include_activesupport_testing_tagged_logger.rb b/snippets/include_activesupport_testing_tagged_logger.rb index e349818a3b..dfd99ebea9 100644 --- a/snippets/include_activesupport_testing_tagged_logger.rb +++ b/snippets/include_activesupport_testing_tagged_logger.rb @@ -57,12 +57,11 @@ class TestError < StandardError; end it 'raises the explicitly thrown error' do allow_any_instance_of(TestJob).to receive(:perform).and_raise(TestError) - test_error = TestError.new('foo') # Rails 6.1+ wraps unexpected errors in tests expected_error = if Rails::VERSION::STRING.to_f >= 6.1 - Minitest::UnexpectedError.new(test_error) + Minitest::UnexpectedError.new(TestError) else - test_error + TestError end expect { perform_enqueued_jobs { TestJob.perform_later } }