RSpec for Sidekiq Pro
Installation
bundle add rspec-sidekiq_pro --group=testConfiguration
rspec/sidekiq_pro requires sidekiq/testing by default so there is no need to include it.
It also means that Sidekiq in set to fake mode by default. Take a look at Sidekiq wiki for more details.
If you wish to start each spec without enqueued jobs or batches:
require "rspec/sidekiq_pro"
RSpec.configure do |config|
config.before do
Sidekiq::Queues.clear_all
RSpec::SidekiqPro::Batches.clear_all
end
endUsage
Two matchers are provided:
-
enqueue_sidekiq_jobsupports block expectation -
have_enqueued_sidekiq_jobsupports value expectation
it do
expect { SampleJob.perform_async }.to enqueue_sidekiq_job(SampleJob)
endit do
SampleJob.perform_async
expect(SampleJob).to have_enqueued_sidekiq_job
endBoth matchers provide the same chainable methods:
.with.once.twice.exactly(n).times.more_than(n).times.less_than(n).times.at_least(n).times.at_most(n).times.in.at.within_batch.without_batch
Checking arguments
it do
expect { SampleJob.perform_async(1, 2, 3) }
.to enqueue_sidekiq_job(SampleJob).with(1, 2, 3)
endit do
SampleJob.perform_async(1, 2, 3)
expect(SampleJob).to have_enqueued_sidekiq_job.with(1, 2, 3)
endChecking counts
it do
expect { SampleJob.perform_async }
.to enqueue_sidekiq_job(SampleJob).once
endit do
expect {
2.times { SampleJob.perform_async }
}.to enqueue_sidekiq_job(SampleJob).twice
endit do
expect {
3.times { SampleJob.perform_async }
}.to enqueue_sidekiq_job(SampleJob).exactly(3).times
endit do
expect {
10.times { SampleJob.perform_async }
}.to enqueue_sidekiq_job(SampleJob).more_than(5).times
endBe careful when checking both counts and arguments:
it do
expect {
SampleJob.perform_async.with(1)
SampleJob.perform_async.with(2)
}
.to enqueue_sidekiq_job(SampleJob).twice
.and enqueue_sidekiq_job(SampleJob).once.with(1)
.and enqueue_sidekiq_job(SampleJob).once.with(2)
endChecking schedules
it do
expect {
SampleJob.perform_in(5.minutes)
}.to enqueue_sidekiq_job(SampleJob).in(5.minutes)
endit do
expect {
SampleJob.perform_at(10.minutes.from_now)
}.to enqueue_sidekiq_job(SampleJob).at(10.minutes.from_now)
endTime matching is performed to the second, if you have code that takes some time to be executed consider using Timecop.
Batches
it do
expect {
SampleJob.perform_async
}.to enqueue_sidekiq_job(SampleJob).without_batch
endit do
expect {
batch = Sidekiq::Batch.new
batch.jobs { SampleJob.perform_async }
}.to enqueue_sidekiq_job(SampleJob).within_batch
endit do
expect { start_some_complex_workflow }
.to enqueue_sidekiq_job(SampleJob).twice.within_batch { |batch|
expect(batch).to have_attributes(description: "Complex Workflow first step")
}
endwithin_batch and without_batch require Sidekiq::Testing to be enabled.
When Sidekiq::Testing is enabled, every batch is pushed to RSpec::SidekiqPro::Batches instead of Redis.
it do
batch = Sidekiq::Batch.new
batch.jobs { SampleJob.perform_async }
expect(RSpec::SidekiqPro::Batches.first.bid).to eq(batch.bid)
endContributing
- Don't hesitate to submit your feature/idea/fix in issues
- Fork the repository
- Create your feature branch
- Ensure RSpec & Rubocop are passing
- Create a pull request
Tests & lint
bundle exec rspec
bundle exec rubocop
bundle exec standardrbAll of them can be run with:
bundle exec rakeLicense & credits
Please see LICENSE for further details.
Inspired by the philostler/rspec-sidekiq & pirj/rspec-enqueue_sidekiq_job
Contributors: ./graphs/contributors