测试调用延迟作业

Test that Delayed Job is called

我正在尝试测试我的部分代码是 运行 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 = false。 规格:

it '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

我哪里做错了,我该如何测试?

这只是我在订单中犯的错误: expect(Restream::StartAllJob).to receive(:new)应该写在post :start_restream, :id => channel.id

之前