Message expectation counts don't work in combination with a stub #28

Closed
myronmarston opened this Issue Nov 2, 2010 · 1 comment

2 participants

@myronmarston
RSpec member

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.

@dnurzynski

Fix bug: Message expectation counts don't work in combination with a stub

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment