我有以下工人
class JobBlastingWorker
include Sidekiq::Worker
sidekiq_options queue: 'job_blasting_worker'
def perform(job_id, action=nil)
job = Job.find(job_id)
JobBlastingService.new(job).call
sidekiq_id = JobBlastingWorker.perform_in(2.minutes, job.id, 're-blast', true)
job.sidekiq_trackers.create(sidekiq_id: sidekiq_id, worker_type: 'blast_version_update')
end
end
在我的 rspec 测试中,我有以下内容job_blasting_worker_spec.erb
require 'rails_helper'
describe JobBlastingWorker do
before(:all) do
Rails.cache.clear
end
describe 'perform' do
context 'create' do
it 'creates job schedule for next 2mins' do
@job = create(:job)
worker = JobBlastingWorker.new
expect(JobBlastingWorker).to have_enqueued_sidekiq_job(@job.id, 're-blast').in(2.minutes)
worker.perform(@job.id, 'create')
end
end
end
end
我希望这能奏效,但我意识到应该安排在接下来的 2 分钟内的 sidekiq 作业永远不会被创建。因此,测试失败。
我如何确保 sidekiq 作业在接下来的 2 分钟内实际创建并且测试成功运行?