Last Updated: February 25, 2016
·
1.043K
· vbrodsky

Rspec Matcher for delayed jobs

Have you ever tried to confirm that a particular delayed job would be fired in rspec?

Now you can, with this custom matcher:

RSpec::Matchers.define :startdelayedjobs do |*expected|

 match_for_should do |delayed_jobs|
     jobs = Delayed::Backend::ActiveRecord::Job.all.map do |job|
     YAML.load(job.handler).method_name
end
expected.any? do |e|
  jobs.include?(e)
end

end

match_for_should_not do |delayed_jobs|

     jobs = Delayed::Backend::ActiveRecord::Job.all.map do |job|
     YAML.load(job.handler).method_name
  end

expected.none? do |e|
     jobs.include?(e)
 end
end

end

so you can do things like this:

object.should_not start_delayed_jobs(:method)