From cb1d2f02515a6870c4898b083ab51c497c125df3 Mon Sep 17 00:00:00 2001 From: Will Spurgin Date: Fri, 27 Oct 2023 14:04:36 -0500 Subject: [PATCH] Include composable module in base matcher (#211) --- README.md | 13 +++++++++++-- lib/rspec/sidekiq/matchers/base.rb | 1 + lib/rspec/sidekiq/matchers/enqueue_sidekiq_job.rb | 11 +++++++++-- .../sidekiq/matchers/enqueue_sidekiq_job_spec.rb | 10 ++++++++++ .../matchers/have_enqueued_sidekiq_job_spec.rb | 8 ++++++++ 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f24d6c3..66f2ec9 100644 --- a/README.md +++ b/README.md @@ -75,11 +75,17 @@ expect { AwesomeJob.perform_at(specific_time, "Awesome!") }.to( .on("default") .at(specific_time) ) + +# Also composable +expect do + AwesomeJob.perform_async + OtherJob.perform_async +end.to enqueue_sidekiq_job(AwesomeJob).and enqueue_sidekiq_job(OtherJob) ``` ### have_enqueued_sidekiq_job -*Describes that there should be an enqueued job with the **specified -arguments*** + +Describes that there should be an enqueued job with the **specified arguments** ```ruby AwesomeJob.perform_async 'Awesome', true @@ -95,6 +101,9 @@ AwesomeJob.perform_async({"something" => "Awesome", "extra" => "stuff"}) expect(AwesomeJob).to have_enqueued_sidekiq_job(hash_including("something" => "Awesome")) expect(AwesomeJob).to have_enqueued_sidekiq_job(any_args) expect(AwesomeJob).to have_enqueued_sidekiq_job(hash_excluding("bad_stuff" => anything)) + +# composable as well +expect(AwesomeJob).to have_enqueued_sidekiq_job(any_args).and have_enqueued_sidekiq_job(hash_including("something" => "Awesome")) ``` #### Testing scheduled jobs diff --git a/lib/rspec/sidekiq/matchers/base.rb b/lib/rspec/sidekiq/matchers/base.rb index 18f8134..b8658bc 100644 --- a/lib/rspec/sidekiq/matchers/base.rb +++ b/lib/rspec/sidekiq/matchers/base.rb @@ -162,6 +162,7 @@ def unwrap_jobs(jobs) # @api private class Base include RSpec::Mocks::ArgumentMatchers + include RSpec::Matchers::Composable attr_reader :expected_arguments, :expected_options, :klass, :actual_jobs diff --git a/lib/rspec/sidekiq/matchers/enqueue_sidekiq_job.rb b/lib/rspec/sidekiq/matchers/enqueue_sidekiq_job.rb index 32b29fc..be30aa6 100644 --- a/lib/rspec/sidekiq/matchers/enqueue_sidekiq_job.rb +++ b/lib/rspec/sidekiq/matchers/enqueue_sidekiq_job.rb @@ -56,7 +56,8 @@ def supports_block_expectations? # # Passes if a Job is enqueued as the result of a block. Chainable `with` # for arguments, `on` for queue, `at` for queued for a specific time, and - # `in` for a specific interval delay to being queued, `immediately` for queued without delay. + # `in` for a specific interval delay to being queued, `immediately` for + # queued without delay. # # @example # @@ -83,7 +84,13 @@ def supports_block_expectations? # # Without any delay # expect { AwesomeJob.perform_async }.to enqueue_sidekiq_job.immediately # expect { AwesomeJob.perform_at(1.hour.ago) }.to enqueue_sidekiq_job.immediately - + # + # ## Composable + # + # expect do + # AwesomeJob.perform_async + # OtherJob.perform_async + # end.to enqueue_sidekiq_job(AwesomeJob).and enqueue_sidekiq_job(OtherJob) def enqueue_sidekiq_job(job_class = nil) EnqueueSidekiqJob.new(job_class) end diff --git a/spec/rspec/sidekiq/matchers/enqueue_sidekiq_job_spec.rb b/spec/rspec/sidekiq/matchers/enqueue_sidekiq_job_spec.rb index ab35266..e2e9d0d 100644 --- a/spec/rspec/sidekiq/matchers/enqueue_sidekiq_job_spec.rb +++ b/spec/rspec/sidekiq/matchers/enqueue_sidekiq_job_spec.rb @@ -197,6 +197,16 @@ end end + describe "composable" do + let(:other_worker) { create_worker } + it "can be composed with other matchers" do + expect do + worker.perform_async + other_worker.perform_async + end.to enqueue_sidekiq_job(worker).and enqueue_sidekiq_job(other_worker) + end + end + describe "chainable" do it "can chain expectations on the job" do specific_time = 1.hour.from_now diff --git a/spec/rspec/sidekiq/matchers/have_enqueued_sidekiq_job_spec.rb b/spec/rspec/sidekiq/matchers/have_enqueued_sidekiq_job_spec.rb index a767622..92fd11d 100644 --- a/spec/rspec/sidekiq/matchers/have_enqueued_sidekiq_job_spec.rb +++ b/spec/rspec/sidekiq/matchers/have_enqueued_sidekiq_job_spec.rb @@ -82,6 +82,14 @@ end end end + + describe "composable" do + it "can be composed with other matchers" do + worker.perform_async(*worker_args) + worker.perform_async(1) + expect(worker).to have_enqueued_sidekiq_job(*worker_args).and have_enqueued_sidekiq_job(1) + end + end end context 'ActiveJob' do