Search code examples
ruby-on-railsrubyrspecmatcher

Rspec is it possible to do something like should_receive(:foo).with(:bar).and_call_original.exacly(1).times?


Is it possible to do something like ServiceObject.should_receive(:foo).with(:bar).and_call_original.exacly(1).times?

My specs look something like this:

it 'should call instance of service object\'s :baz! method' do
  ServiceObject.any_instance.should_receive(:baz!).exactly(1).times
end

it 'should call service object\'s :foo method' do
  ServiceObject.should_receive(:foo).with(:bar).and_call_original.exacly(1).times
end

If I remove and_call_original the first spec fails. If I comment out the .exacly(1).times in the second spec both specs pass.

Two questions:

  • Why do these two specs interfere with one another?
  • Is there a way to call something that means .should_receive(:foo).with(:bar).and_call_original.exacly(1).times?

Thanks in advance!


Solution

  • and_call_original doesn't return the current example so it cannot work.

    Instead you can do:

    .should_receive(:foo).with(:bar).exactly(1).times.and_call_original