Incorporating Delayed Job in rspec specs
Update: The preferred way to do this these days is simply: Delayed::Worker.new.work_off
. . .
I wanted to test the results of some Delayed Job enabled methods in my specs. DJ has a nice method Delayed::Job.work_off that does all of the jobs on the queue. Unfortunately, it does them in a separate thread, so if you run it from rspec and there are errors in your jobs, rspec never hears about them. My solution was to write an even simpler, in-thread job runner: (edit: updated to delete the job too, at Damien’s suggestion)
# lib/delayed_job_spec_helper.rb module DelayedJobSpecHelper def work_off Delayed::Job.all.each do |job| job.payload_object.perform job.destroy end end end
My spec might look like this:
describe Thingy do include DelayedJobSpecHelper it "should have been worked on if I do something that queues jobs" do thing = Thingy.new thing.method_that_queues_jobs work_off thing.should be_worked_on end end