Rspec 测试delayed_job

问题描述 投票:0回答:3

我的应用程序中有一些复杂的、长时间运行的delayed_job进程。我正在使用 Rspec 来测试流程中使用的各个方法和类,但我也想使用不同的测试数据执行许多端到端后台作业。

我在delayed_job wiki上找不到任何关于此的内容,这个问题看起来很有趣,但我真的不明白这里发生了什么。 使用rSpec测试delayed_job链的最佳方法是什么?

我可以轻松地使用工厂设置测试数据,然后调用启动后台处理的类。我预计测试需要很长时间才能完成。

编辑后台代码

class Singleplex
    def perform(batch_id,user)   
      batch = start_batch(batch_id,user)
        ... do lots of stuff ...
    end
    handle_asynchronously :perform, queue: :singleplex, :run_at => Proc.new { 1.second.from_now }

规格/工厂/批次.rb

FactoryGirl.define do
  factory :batch do
    batch_type 'singleplex'
    name 'valid panel'
    status 'ready'
  end

  factory :batch_detail do
    chrom 7
    chrom_start 140435012
    chrom_end 140435012
    target_offset 150
    padding 4
    primer3_parameter_id 1
    snp_mask 't'
    status 'ready'
    batch
  end
end

然后像这样运行测试

describe Batch do  
  it 'runs Singleplex for a valid panel' do
    batch = FactoryGirl.create(:batch)
    user = User.find(1)
    status =  Singleplex.new.perform(batch.id,user)
    expect(status.should == true)
  end
end

我有两个问题需要解决:

1)如何告诉测试等待delayed_job调用完成后再验证结果?

2)为了验证结果,我需要检查多个表中的值。在 Rspec 中执行此操作的最佳方法是什么?

编辑

我应该添加我得到一个delayed_job对象,所以状态检查当然会失败。这些工作通常至少需要 10 分钟。

1) Batch runs Singleplex for a valid panel
     Failure/Error: expect(status.should == true)
       expected: true
            got: #<Delayed::Backend::ActiveRecord::Job id: nil, priority: 0, attempts: 0, handler: "--- !ruby/object:Delayed::PerformableMethod\nobject:...", last_error: nil, run_at: nil, locked_at: nil, failed_at: nil, locked_by: nil, queue: nil, created_at: nil, updated_at: nil> (using ==)
rspec delayed-job
3个回答
42
投票

有几种方法可以做到这一点。所有这些都要求您在代码中执行作业。

方法 1:将作业排队然后告诉

DelayedJob::Worker
完成它的测试。

describe Batch do  
  it 'runs Singleplex for a valid panel' do
    batch = FactoryGirl.create(:batch)
    user = User.find(1)
    Singleplex.new.perform(batch.id,user)
    expect(Delayed::Worker.new.work_off).to eq [1, 0] # Returns [successes, failures]
    # Add expectations which check multiple tables to make sure the work is done
  end
end

方法 2:在禁用队列的情况下运行相关作业并检查所需结果的测试。您可以通过在测试配置中的某个位置或在

Delayed::Worker.delay_jobs = false
块中调用
before
来延迟排队。

before(:each) do
  Delayed::Worker.delay_jobs = false
end
describe Batch do  
  it 'runs Singleplex for a valid panel' do
    batch = FactoryGirl.create(:batch)
    user = User.find(1)
    Singleplex.new.perform(batch.id,user)
    # expectations which check that the work is done
  end
end

但是,众所周知,此方法会 导致回调问题

方法 3:编写一个观察者来监视创建并运行它们的任何新作业。这样您就不必在测试中手动声明“work_off”。 Artsy 有一个要点

在其他地方进行测试也是一个好主意,以确保作业按预期排队

it "queues welcome when a user is created" do
  expect(Delayed::Job.count).to eq 0
  # Create user step
  expect(Delayed::Job.count).to eq 1 # You should really be looking for the count of a specific job.
end

16
投票

如果您想围绕单个测试或一组测试运行延迟作业,您可以将其添加到您的spec_helper.rb

config.around(:each, :run_delayed_jobs) do |example|
  Delayed::Worker.delay_jobs = false

  example.run

  Delayed::Worker.delay_jobs = true
end

并调用它:

it 'runs the job', :run_delayed_jobs do
  # delayed job magic
end

0
投票

我认为任何使用延迟工作的人都会面临这个问题。就我而言,我使用延迟作业以异步模式发送电子邮件通知。在进行单元测试时,我通常需要验证邮件队列工作是否已完成。为了处理这个问题,我为 Rspec 编写了一个自定义匹配器,如下所示:

  RSpec::Matchers.define :have_created_delayed_job do |method_name|
    match do |_|
      Delayed::Job.all.find do |job|
        # rubocop:disable Security/YAMLLoad
        YAML.load(job.handler).method_name == method_name
        # rubocop:enable Security/YAMLLoad
      end
    end
    failure_message do |subject|
      "Expected that #{subject} would have created a delayed job with method name #{method_name}.\n
      But we didn't find the matching job."
    end
  end

然后在你的测试代码中,你可以这样做:

expect(subject).to have_created_delayed_job(:your_method_name)
© www.soinside.com 2019 - 2024. All rights reserved.