Message expectation counts don't work in combination with a stub
Created by: myronmarston
Spec:
class MyClass def initialize(dependency) @dependency = dependency end def calculate_value @dependency.foo end end describe MyClass, '#calculate_value' do let(:dependency) { Object.new } subject { MyClass.new(dependency) } context 'when dependency.foo returns :bar' do before(:each) { dependency.stub(:foo).and_return(:bar) } it 'returns :bar' do subject.calculate_value.should == :bar end it 'memoizes the value so dependency.foo is not invoked multiple times' do dependency.should_receive(:foo).once 3.times { subject.calculate_value } end end end
These specs pass:
$ rspec rspec_example.rb .. Finished in 0.00199 seconds 2 examples, 0 failures
...but the 2nd example should fail. If I comment out the before(:each)
, then it properly fails.