测试是否调用延迟作业

时间:2017-10-13 09:28:07

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

我正在尝试测试我的代码的一部分正在运行DelayedJob。 这是代码:

def start_restream
  ...
  puts 'Here'
  Delayed::Job.enqueue(Restream::StartAllJob.new(channel.id))
  puts 'After'
  ...
end

#app/jobs/restream/start_all_job.rb
class Restream::StartAllJob < Struct.new(:channel_id)
  def perform
    puts "Inside"
    ...
  end
end

spec_helper.rb Delayed::Worker.delay_jobs = falseit 'runs Delayed::Job::Restream::StartAll' do post :start_restream, :id => channel.id expect(Restream::StartAllJob).to receive(:new) end 。 规范:

Here
Inside
After

打印出来

Failure/Error: expect(Restream::StartAllJob).to receive(:new) 

(Restream::StartAllJob (class)).new(*(any args))
       expected: 1 time with any arguments
       received: 0 times with any arguments

运行时,我知道它被调用了。但测试失败了:

expect_any_instance_of(Restream::StartAllJob).to receive(:perform)

或者,如果我使用Failure/Error: example.run Exactly one instance should have received the following message(s) but didn't: perform ,则说

{{1}}

我做错了什么,如何测试呢?

1 个答案:

答案 0 :(得分:0)

这只是我在订单中犯的错误:   应在create

之前撰写expect(Restream::StartAllJob).to receive(:new)