|
| 1 | +if __FILE__ =~ /^snippets/ |
| 2 | + fail "Snippets are supposed to be run from their own directory to avoid side " \ |
| 3 | + "effects as e.g. the root `Gemfile`, or `spec/spec_helpers.rb` to be " \ |
| 4 | + "loaded by the root `.rspec`." |
| 5 | +end |
| 6 | + |
| 7 | +# We opt-out from using RubyGems, but `bundler/inline` requires it |
| 8 | +require 'rubygems' |
| 9 | + |
| 10 | +require "bundler/inline" |
| 11 | + |
| 12 | +# We pass `false` to `gemfile` to skip the installation of gems, |
| 13 | +# because it may install versions that would conflict with versions |
| 14 | +# from the main `Gemfile.lock`. |
| 15 | +gemfile(false) do |
| 16 | + source "https://rubygems.org" |
| 17 | + |
| 18 | + git_source(:github) { |repo| "https://github.com/#{repo}.git" } |
| 19 | + |
| 20 | + # Those Gemfiles carefully pick the right versions depending on |
| 21 | + # settings in the ENV, `.rails-version` and `maintenance-branch`. |
| 22 | + Dir.chdir('..') do |
| 23 | + eval_gemfile 'Gemfile-sqlite-dependencies' |
| 24 | + # This Gemfile expects `maintenance-branch` file to be present |
| 25 | + # in the current directory. |
| 26 | + eval_gemfile 'Gemfile-rspec-dependencies' |
| 27 | + # This Gemfile expects `.rails-version` file |
| 28 | + eval_gemfile 'Gemfile-rails-dependencies' |
| 29 | + end |
| 30 | + |
| 31 | + gem "rspec-rails", path: "../" |
| 32 | +end |
| 33 | + |
| 34 | +# Run specs at exit |
| 35 | +require "rspec/autorun" |
| 36 | + |
| 37 | +require "rails" |
| 38 | +require "active_record/railtie" |
| 39 | +require "active_job/railtie" |
| 40 | +require "rspec/rails" |
| 41 | + |
| 42 | +ActiveJob::Base.queue_adapter = :test |
| 43 | + |
| 44 | +# This connection will do for database-independent bug reports |
| 45 | +ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") |
| 46 | + |
| 47 | +class TestJob < ActiveJob::Base |
| 48 | + def perform; end |
| 49 | +end |
| 50 | + |
| 51 | +class TestError < StandardError; end |
| 52 | + |
| 53 | +RSpec.describe 'Foo', type: :job do |
| 54 | + include ::ActiveJob::TestHelper |
| 55 | + |
| 56 | + describe 'error raised in perform_enqueued_jobs with block' do |
| 57 | + it 'raises the explicitly thrown error' do |
| 58 | + allow_any_instance_of(TestJob).to receive(:perform).and_raise(TestError) |
| 59 | + |
| 60 | + # Rails 6.1+ wraps unexpected errors in tests |
| 61 | + expected_error = if Rails::VERSION::STRING.to_f >= 6.1 |
| 62 | + Minitest::UnexpectedError.new(TestError) |
| 63 | + else |
| 64 | + TestError |
| 65 | + end |
| 66 | + |
| 67 | + expect { perform_enqueued_jobs { TestJob.perform_later } } |
| 68 | + .to raise_error(expected_error) |
| 69 | + end |
| 70 | + end |
| 71 | +end |
0 commit comments