16

This is my scenario:

After updating an AR object, it fires a bunch of background jobs with Resque. In my specs I’m mocking the call to Resque#enqueue, something in the lines of:

it 'should be published' do
  # I need to setup these mocks in many places where I want to mock a specific call to Resque, otherwise it fails
  Resque.should_receive(:enqueue).with(NotInterestedJob1, anything)
  Resque.should_receive(:enqueue).with(NotInterestedJob2, anything)
  Resque.should_receive(:enqueue).with(NotInterestedJob3, anything)

  # I'm only interested in mocking this Resque call.
  Resque.should_receive(:enqueue).with(PublishJob, anything)
end

As you can see, I need to mock all other calls to Resque#enqueue everytime I want to mock a specific call, is there a way to only mock a custom call and ignore the other calls with different arguments?

Thanks in advance ;)

infused
  • 24,000
  • 13
  • 68
  • 78
rdavila
  • 370
  • 3
  • 9

1 Answers1

19

I think that in this case you would need to do what I think of as the method stubbing equivalent of as_null_object, but in this case specifically for calls to Resque.enqueue that you don't care about:

it 'should be published' do
  allow(Resque).to receive(:enqueue) # stub out this message entirely
  expect(Resque).to receive(:enqueue).with(PublishJob, anything)
  call_your_method.that_calls_enqueue
end
Iulian Onofrei
  • 9,188
  • 10
  • 67
  • 113
Paul Fioravanti
  • 16,423
  • 7
  • 71
  • 122
  • 1
    Thanks for your help Paul, I'm using Rspec 2 so #allow and #receive are not available but I've replaced them with `Resque.stub(:enqueue).and_return(true)` and `Resque.should_receive(:enqueue).with(PublishJob, anything)` – rdavila Sep 02 '14 at 16:45