Delayed :: Worker.new.work_off是否在未来的run_at时间内完成工作?

时间:2013-02-28 04:46:40

标签: ruby-on-rails-3 rspec delayed-job

我有一个控制器操作,可以创建2个后续作业,以便在将来运行。

我正在尝试测试后台作业是否正常运行

    # spec/controllers/job_controller_spec.rb

    setup
    post :create, {:job => valid_attributes}
    Delayed::Job.count.should == 2
    Delayed::Worker.logger = Rails.logger
    #Delayed::Worker.new.work_off.should == [2,0]

    Delayed::Worker.new.work_off
    Delayed::Job.count.should == 0 # this is where it fails

这是错误:

 1) JobsController POST create with valid params queues up delayed job and fires
    Failure/Error: Delayed::Job.count.should == 0
    expected: 0
    got: 2 (using ==)

出于某种原因,它似乎没有被解雇。

1 个答案:

答案 0 :(得分:3)

您可以尝试使用

Delayed::Worker.new(quiet: false).work_off

调试后台作业的结果,这可以帮助你找出他们应该在将来运行的事实是否正在搞乱断言本身。 完成后不要忘记取消“quiet:false”,否则测试将始终输出后台作业的结果。