How to check delayed_job callback hooks using RSpec

I would like to verify that delayed_job callbacks are called, but I don’t see how to get RSpec to do this, especially when multiple class layers are involved.

Assuming I have an ActiveRecord model:

class MyJob < ActiveRecord::Base
  def perform
    # do some stuff
  end
  def after(job)
    # called by delayed_job after perform completes
  end
end

Conceptually, I need the RSpec test in these lines (although I know this is wrong):

it 'should call the :after callback hook' do
  my_job = MyJob.create
  my_job.should_receive(:after).with(an_instance_of(Delayed::Backend::ActiveRecord::Job))

  Delayed::Job.enqueue(my_job)
  Delayed::Worker.new.work_off
end

I looked at stackoverflow, relishapp.com/rspec and all the other places that I can think of. It can't be too hard, can it? (I bet @zetetic knows the answer in a dream ...;)

(Caution: in my actual case, the “strip” class is used between MyJob and DJ. The answer to this simple case may cause a more complicated continuation!)

+5
2

, , , ...

, DelayedJob, , DJ .

, DelayedJob? , , , MockedJob , /...

+1

, , Jobs , . - .

it "should sync content from the remote CMS" do      
  DelayedJobs::CMS::Sync.module_eval do
  def perform
    url.should == "http://example.com/tools/datafeed/resorts/all"
    Report.sync!([{'id' => 1, 'name' => 'resort foo'}])
  end
end

lambda do
  Report.sync_all!
end.should change(Delayed::Job, :count)

lambda do
  Delayed::Job.work_off
end.should change(Resort, :count).by(2)
end
+1

All Articles