【发布时间】:2014-12-22 00:40:57
【问题描述】:
我的应用程序中有一些复杂、长时间运行的延迟作业进程。我正在使用 Rspec 来测试流程中使用的各个方法和类,但我也想使用不同的测试数据执行许多端到端的后台作业。
我在delayed_job wiki 上找不到任何关于此的内容,这个SO 问题看起来很有趣,但我并不真正了解这里发生了什么。 What's the best way to test delayed_job chains with rSpec?
我可以很容易地用工厂设置测试数据,然后调用启动后台处理的类。我预计测试需要很长时间才能完成。
编辑后台代码
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 }
spec/factories/batches.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) 如何告诉测试等到 delay_job 调用完成后再验证结果?
2) 为了验证结果,我需要检查多个表中的值。在 Rspec 中执行此操作的最佳方法是什么?
编辑
我应该补充一下,我得到了一个 delay_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 的
should和expect语法。expect(status.should == true)应该是status.should == true或expect(status).to == true
标签: rspec delayed-job