From f54e55884536ed3b0975114cabdeb9a46752a313 Mon Sep 17 00:00:00 2001 From: Shane Cavanaugh Date: Wed, 9 Mar 2022 17:07:38 -0700 Subject: [PATCH 1/2] Include ActiveSupport::Testing::TaggedLogging for Rails >= 7 --- .../rails/example/rails_example_group.rb | 5 ++ ...ude_activesupport_testing_tagged_logger.rb | 71 +++++++++++++++++++ .../rails/example/rails_example_group_spec.rb | 10 +++ 3 files changed, 86 insertions(+) create mode 100644 snippets/include_activesupport_testing_tagged_logger.rb 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/snippets/include_activesupport_testing_tagged_logger.rb b/snippets/include_activesupport_testing_tagged_logger.rb new file mode 100644 index 0000000000..dfd99ebea9 --- /dev/null +++ b/snippets/include_activesupport_testing_tagged_logger.rb @@ -0,0 +1,71 @@ +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:") + +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 6.1+ wraps unexpected errors in tests + expected_error = if Rails::VERSION::STRING.to_f >= 6.1 + Minitest::UnexpectedError.new(TestError) + else + TestError + end + + expect { perform_enqueued_jobs { TestJob.perform_later } } + .to raise_error(expected_error) + 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 4a80dda74b81a48ba9d1419ca792588aed5f8337 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Mon, 11 Apr 2022 19:42:54 +0300 Subject: [PATCH 2/2] Define name to work around TaggedLogging RSpec.current_example.metadata[:name] evaluates to `nil` by the time this is called. --- lib/rspec/rails/adapters.rb | 10 ++++++++++ lib/rspec/rails/example/rails_example_group.rb | 6 +----- .../include_activesupport_testing_tagged_logger.rb | 11 +++++------ 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/rspec/rails/adapters.rb b/lib/rspec/rails/adapters.rb index 1990b797e4..316724746d 100644 --- a/lib/rspec/rails/adapters.rb +++ b/lib/rspec/rails/adapters.rb @@ -181,5 +181,15 @@ def assertion_delegator # # @private TestUnitAssertionAdapter = MinitestAssertionAdapter + + # @private + module TaggedLoggingAdapter + require 'active_support/testing/tagged_logging' + include ActiveSupport::Testing::TaggedLogging + + # Just a stub as TaggedLogging is calling `name` + def name + end + end end end diff --git a/lib/rspec/rails/example/rails_example_group.rb b/lib/rspec/rails/example/rails_example_group.rb index 2a09730bc6..c3d42a527c 100644 --- a/lib/rspec/rails/example/rails_example_group.rb +++ b/lib/rspec/rails/example/rails_example_group.rb @@ -12,11 +12,7 @@ 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 + include RSpec::Rails::TaggedLoggingAdapter if ::Rails::VERSION::MAJOR >= 7 end end end diff --git a/snippets/include_activesupport_testing_tagged_logger.rb b/snippets/include_activesupport_testing_tagged_logger.rb index dfd99ebea9..c8d87caf34 100644 --- a/snippets/include_activesupport_testing_tagged_logger.rb +++ b/snippets/include_activesupport_testing_tagged_logger.rb @@ -20,7 +20,6 @@ # 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' @@ -44,19 +43,19 @@ # This connection will do for database-independent bug reports ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") +class TestError < StandardError; end + class TestJob < ActiveJob::Base - def perform; end + def perform + raise TestError + 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 6.1+ wraps unexpected errors in tests expected_error = if Rails::VERSION::STRING.to_f >= 6.1 Minitest::UnexpectedError.new(TestError)