Mock Sidekiq Pro Batches
mainFor Sidekiq Pro users, you can opt-in to mocking the Batch implementation by setting stub_batches: true. This uses a NullObject pattern, allowing you to test code that uses Sidekiq::Batch without requiring a Redis instance.
Caution: Because it uses a NullObject pattern, the mocked Sidekiq::Batch implementation responds to any method call. It does not provide the exact API of the real Sidekiq::Batch, so calling non-existent methods on the batch object will not raise a NoMethodError unless you explicitly expect it (as shown in the example).
RSpec.describe "Using mocked batches", stub_batches: true do
it "uses mocked batches" do
batch = Sidekiq::Batch.new
batch.jobs do
SomeJob.perform_async 123
end
expect(SomeJob).to have_enqueued_sidekiq_job
# Caution, the NullObject pattern means that the mocked Batch implementation
# responds to anything... even if it's not on the true `Sidekiq::Batch` API
expect { batch.foobar! }.to raise_error(NoMethodError)
end
end