I've had luck with rending the backtrace of the error to an email, which at least gives me some context of when / how the delayed job is failing:
Here is an example:
result = Delayed::Job.work_off
unless result[1].zero?
ExceptionMailer.deliver_exception_message("[Delayed Job Failed] Error id: #{Delayed::Job.last.id}", Delayed::Job.last.last_error)
end
If you just want to write tests for your Delayed::Job tasks here is the approach I have taken. I will stub out the perform task with the expectations from the various scenarios and then test how Delayed::Job handles those results. Here is an example of how I used Delayed::Job to sync with a remote CMS nightly.
it "should sync content from the remote CMS" do
CMSJobs::Sync.module_eval do
def perform
url.should == "http://someurl.com/tools/datafeed/resorts/all"
Resort.sync_resorts!([{'id' => 1, 'name' => 'resort foo'}, { 'id' => 2, 'name' => 'resort bar' }])
end
end
lambda do
Resort.sync_all!
end.should change(Delayed::Job, :count)
lambda do
Delayed::Job.work_off
end.should change(Resort, :count).by(2)
# It should now delete a resort if it doesn't appear in the next CMS feed.
lambda do
Resort.sync_resorts!([{ 'id' => 2, 'name' => 'resort bar' }])
end.should change(Resort, :count).by(-1)
end