Skip to content

Instantly share code, notes, and snippets.

@kpshek
Created July 11, 2012 04:44
Show Gist options
  • Select an option

  • Save kpshek/3088080 to your computer and use it in GitHub Desktop.

Select an option

Save kpshek/3088080 to your computer and use it in GitHub Desktop.
Simple example program illustrating the questions around rspec-mocks issue #158
# coding: UTF-8
require 'rspec'
class Calc
def add(x, y)
puts 'Real implementation called'
x + y
end
end
describe Calc, '#add' do
# Fails on rspec and rspec-mocks-2.10.1
# This will pass on rspec and rspec-mocks 2.9.0
it 'has a stub and should_receive defined' do
calc = double('calc')
calc.should_receive(:add).with(1, 2) do |x, y|
puts 'should_receive called'
end
calc.stub(:add) do |x, y|
puts 'Mock implementation called'
100 # return some arbitrary mock value
end
mock_result = calc.add 1, 2
# This expectation will fail as the #add stub is never called.
# As a result, should_receive is the only thing called at that returns nil
mock_result.should eq 100
end
# Passes always
it 'has just a stub defined' do
calc = double('calc')
calc.stub(:add) do |x, y|
puts 'Mock implementation called'
100 # return some arbitrary mock value
end
mock_result = calc.add 1, 2
# This expectation will pass
mock_result.should eq 100
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment