0

我有以下工人

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

我也有以下job_blasting_service

class JobBlastingService
  def initialize(job)
    @job = job
  end

  def call
    ScoringWorker.perform_async(@job.id, 0, 'create')
  end
end

并且在scoring_worker

class ScoringWorker
  include Sidekiq::Worker
  sidekiq_options queue: 'scoring_worker'

  def perform(job_id, version, action=nil)
    job = Job.find(job_id)

    job.version = version
    job.save
  end
end

现在我想编写一个 rspec 测试来验证发送到的参数ScoringWorker是否准确。

require 'rails_helper'

describe JobBlastingWorker do
  before(:all) do
    Rails.cache.clear
  end

  describe 'perform' do
    context 'create' do
      it 'sends correct arguments for scoring worker' do
        @job = create(:job)
        expect(ScoringWorker).to receive(:perform_async).with(@job.id, 0, 'create').and_call_original
        worker = JobBlastingWorker.new
        expect(JobBlastingWorker).to receive(:perform_in)
        expect(ScoringWorker).to receive(:perform_async).exactly(1).times
        worker.perform(@job.id, 'create')
      end
    end
  end
end

我有这个 rspec 测试,当我运行它时,我收到一个错误,因为它没有任何带有参数的 sidekiq 作业。

我怎样才能为它编写这个测试以获得所需的响应

4

1 回答 1

0

如果你只是想验证你发送的参数是否正确,这样的事情应该会让你接近:

 subject(:job_blasting_worker) { described_class.perform_later(job_id, version, action) }

  let(:job) { create :job }

  it 'queues the job' do
    expect { job_blasting_worker }.to have_enqueued_job(described_class).with(
      job.id,
      0,
      'create'
    ).on_queue(if you have different queues)
  end
end
于 2021-09-23T04:21:48.730 回答