Skip to content

Commit

Permalink
Include composable module in base matcher (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
wspurgin authored Oct 27, 2023
1 parent 2cc9973 commit cb1d2f0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/sidekiq/matchers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions lib/rspec/sidekiq/matchers/enqueue_sidekiq_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/rspec/sidekiq/matchers/enqueue_sidekiq_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/rspec/sidekiq/matchers/have_enqueued_sidekiq_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cb1d2f0

Please sign in to comment.