Search code examples
ruby-on-railsrubyrspecmockingresque

How to ignore some calls to the same method with different argument in Rspec?


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 ;)


Solution

  • 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