Techioz Blog

Rspec、 should_receive(:foo).with(:bar).and_call_original.exacly(1).times のようなことは可能ですか?

概要

ServiceObject. should_receive(:foo).with(:bar).and_call_original.exacly(1).times のようなことは可能ですか?

私のスペックはこんな感じです。

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

and_call_original を削除すると、最初の仕様は失敗します。 2 番目の仕様の .exacly(1).times をコメントアウトすると、両方の仕様が合格します。

2 つの質問:

前もって感謝します!

解決策

and_call_original は現在の例を返さないため、機能しません。

代わりに、次のことができます。

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