diff --git a/actionmailer/test/test_helper_test.rb b/actionmailer/test/test_helper_test.rb index 60fea64999843..c2d18c34792f3 100644 --- a/actionmailer/test/test_helper_test.rb +++ b/actionmailer/test/test_helper_test.rb @@ -623,3 +623,17 @@ def test_setup_shouldnt_conflict_with_mailer_setup assert_equal "a value", @test_var end end + +class AdapterIsNotTestAdapterTest < ActionMailer::TestCase + def queue_adapter_for_test + ActiveJob::QueueAdapters::InlineAdapter.new + end + + def test_can_send_email_using_any_active_job_adapter + assert_nothing_raised do + assert_emails 1 do + TestHelperMailer.test.deliver_now + end + end + end +end diff --git a/activejob/CHANGELOG.md b/activejob/CHANGELOG.md index b46dc2088e83d..6bc2099af7e8a 100644 --- a/activejob/CHANGELOG.md +++ b/activejob/CHANGELOG.md @@ -1,3 +1,10 @@ +* `perform_enqueued_jobs` is now compatible with all Active Job adapters + + This means that methods that depend on it, like Action Mailer's `assert_emails`, + will work correctly even if the test adapter is not used. + + *Alex Ghiculescu* + * Allow queue adapters to provide a custom name by implementing `queue_adapter_name` *Sander Verdonschot* diff --git a/activejob/lib/active_job/test_helper.rb b/activejob/lib/active_job/test_helper.rb index 6f4fc0c4282a4..49a52662f55eb 100644 --- a/activejob/lib/active_job/test_helper.rb +++ b/activejob/lib/active_job/test_helper.rb @@ -595,9 +595,14 @@ def assert_performed_with(job: nil, args: nil, at: nil, queue: nil, priority: ni # # If the +:at+ option is specified, then only run jobs enqueued to run # immediately or before the given time + # + # If an adapter other than the test adapter is in use, this method just yields. + # See queue_adapter_for_test for more information. def perform_enqueued_jobs(only: nil, except: nil, queue: nil, at: nil, &block) return flush_enqueued_jobs(only: only, except: except, queue: queue, at: at) unless block_given? + return _assert_nothing_raised_or_warn("perform_enqueued_jobs", &block) unless using_test_adapter? + validate_option(only: only, except: except) old_perform_enqueued_jobs = queue_adapter.perform_enqueued_jobs @@ -636,12 +641,16 @@ def queue_adapter end private + def using_test_adapter? + queue_adapter.is_a?(ActiveJob::QueueAdapters::TestAdapter) + end + def clear_enqueued_jobs - enqueued_jobs.clear + enqueued_jobs.clear if using_test_adapter? end def clear_performed_jobs - performed_jobs.clear + performed_jobs.clear if using_test_adapter? end def jobs_with(jobs, only: nil, except: nil, queue: nil, at: nil) diff --git a/activejob/test/cases/test_helper_test.rb b/activejob/test/cases/test_helper_test.rb index ac6acf3c29e22..dfef1c8e5e170 100644 --- a/activejob/test/cases/test_helper_test.rb +++ b/activejob/test/cases/test_helper_test.rb @@ -2059,6 +2059,20 @@ def test_assert_performed_with_without_block_does_not_change_jobs_count end end +class AdapterIsNotTestAdapterTest < ActiveJob::TestCase + def queue_adapter_for_test + ActiveJob::QueueAdapters::InlineAdapter.new + end + + def test_perform_enqueued_jobs_just_yields + JobBuffer.clear + perform_enqueued_jobs do + HelloJob.perform_later("kevin") + end + assert_equal(1, JobBuffer.values.size) + end +end + class OverrideQueueAdapterTest < ActiveJob::TestCase class CustomQueueAdapter < ActiveJob::QueueAdapters::TestAdapter; end