如何在Rspec中忽略对不同参数的同一方法的一些调用?

rda*_*ila 12 ruby rspec ruby-on-rails mocking resque

这是我的情景:

更新AR对象后,它会使用Resque触发一堆后台作业.在我的规格中,我正在嘲笑对Resque#enqueue的调用,其中包含以下内容:

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
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我需要模拟所有其他调用Resque #enqueue每次我想模拟一个特定的调用,有没有办法只模拟自定义调用并忽略其他调用不同的参数?

提前致谢 ;)

Pau*_*nti 15

我认为在这种情况下你需要做我认为的方法存根相当于as_null_object,但在这种情况下专门针对Resque.enqueue你不关心的调用:

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
Run Code Online (Sandbox Code Playgroud)