我正在尝试测试我的代码的一部分正在运行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
,则说
{{1}}
我做错了什么,如何测试呢?
答案 0 :(得分:0)
这只是我在订单中犯的错误:
应在create
expect(Restream::StartAllJob).to receive(:new)