2017-10-13 121 views
0

我想測試我的代碼的一部分正在運行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 

什麼我做錯了,我怎麼能考這個?

回答

0

這只是我的順序所犯的錯誤: expect(Restream::StartAllJob).to receive(:new)應該post :start_restream, :id => channel.id

前寫