Sidekiq Batches,Ruby On Rails - 如何使用 rspec 编写测试以测试作业,批处理内部从另一个作业重新打开

问题描述 投票:0回答:0
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

我总是遇到这样的问题
nil:NilClass

的未定义方法“jobs”

但工作正常

describe AnotherWorker, 输入: :job do 描述'#perform'做 主题 { 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

结束 结束

ruby-on-rails rspec-rails sidekiq rspec-sidekiq sidekiq-pro
© www.soinside.com 2019 - 2024. All rights reserved.