2

我有一个控制器操作,它创建 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 ==)

由于某种原因,它似乎没有开火。

4

1 回答 1

4

你可以尝试使用

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

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

于 2013-03-26T18:18:20.547 回答