Class: RSpec::Sidekiq::NullBatch

Inherits:
NullObject show all
Defined in:
lib/rspec/sidekiq/batch.rb

Overview

Sidekiq::Batch is a Sidekiq::Pro feature. However the general consensus is that, by defeault, you can’t test without redis. RSpec::Sidekiq includes a “null object” pattern implementation to mock Batches. This will mock Sidekiq::Batch and prevent it from using Redis.

This is opt-in only feature.

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
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from NullObject

#method_missing

Constructor Details

#initialize(bid = nil) ⇒ NullBatch

Returns a new instance of NullBatch.



34
35
36
37
# File 'lib/rspec/sidekiq/batch.rb', line 34

def initialize(bid = nil)
  @bid = bid || SecureRandom.hex(8)
  @callbacks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RSpec::Sidekiq::NullObject

Instance Attribute Details

#bidObject (readonly)

Returns the value of attribute bid.



32
33
34
# File 'lib/rspec/sidekiq/batch.rb', line 32

def bid
  @bid
end

#descriptionObject

Returns the value of attribute description.



31
32
33
# File 'lib/rspec/sidekiq/batch.rb', line 31

def description
  @description
end

Instance Method Details

#jobsObject



47
48
49
# File 'lib/rspec/sidekiq/batch.rb', line 47

def jobs(*)
  yield
end

#on(*args) ⇒ Object



43
44
45
# File 'lib/rspec/sidekiq/batch.rb', line 43

def on(*args)
  @callbacks << args
end

#statusObject



39
40
41
# File 'lib/rspec/sidekiq/batch.rb', line 39

def status
  NullStatus.new(@bid, @callbacks)
end