1
Class ParentWorker
    def perform(team_id)
        batch = Sidekiq::Batch.new
        batch.on(:complete, self.class, {:team_id => team_id})
        batch.jobs do
            Team.find(team_id).assets.each do |asset|
                AnotherWorker.perform_async(asset.id)
            end
    end
    def on_compete(status, options)
       ...
    end
end


class AnotherWorker
    def perform(asset_id)
        batch.jobs do
          3.times do 
            ThirdJobWorker.perform_async
          end
        end
    end
end

I all time get the problem that
undefined method `jobs' for nil:NilClass

But the jobs works correct

describe AnotherWorker, type: :job do describe '#perform' do subject { described_class.new.perform(asset_id) }

let(:asset_id) { create(:asset).id }

let(:batch) { instance_double(Sidekiq::Batch) }


before do
  allow(Sidekiq::Batch).to receive(:new).and_return(batch)
  allow(batch).to receive(:jobs).and_yield
end

it 'call the other jobs' do
  expect(batch).to receive(:jobs).and_yield

  subject

  expect(ThirdJobWorker).to have_enqueued_sidekiq_job(asset_id)
end

end end

Anastasiia
  • 11
  • 2
  • I don't really get how you would think this would work. You're haven't defined the local variable `batch` in `AnotherWorker#perform` nor is it a method as far as I can see. Maybe this is just a problem you introduced when you abstracted the original problem? – max Apr 24 '23 at 20:50
  • Also `allow(batch).to receive(:jobs).and_yield` is just stubbing the instance in your spec. It doesn't effect how the class under test behaves. To do that you would need to for example make batch a method that can be stubbed. I think you need a primer on variable scoping or something. https://www.rubyguides.com/2019/03/ruby-scope-binding/ – max Apr 24 '23 at 20:52

0 Answers0