The simplest way to test queued Delayed::Job tasks in RSpec is to run them in real time. Simply add the following line to your RSpec tests:
Delayed::Worker.delay_jobs = false
This will cause your jobs to be processed immediately upon enqueuing, not in a separate thread. This is usually what you want for testing, since it’s deterministic.
Two caveats
-
If you are trying to test for timing errors, race conditions, etc, this approach won’t help (since the jobs are processed in the same thread as RSpec)
-
The current version of delayed_job (2.1.4) has a minor bug in which the callback hooks (enqueue, before, success, error, failure) don’t get called when
Delayed::Worker.delay_jobs
is set to false.
Two Workarounds
If you need to test the callback hooks I know of two workarounds:
-
Fetch the latest master branch from github. (I haven’t tried that because I need a stable version)
-
Instead of setting
Delayed::Worker.delay_jobs = false
, explicitly call DJ’s run mechanism in your test code as follows:successes, failures = Delayed::Worker.new.work_off
That will process whatever is in the job queue (again, in the same thread as the RSpec tests) and return two numbers: the # of jobs that succeeded and the # of jobs that failed. I currently use this approach and it does everything I need.