Skip to content

Instantly share code, notes, and snippets.

@dpetersen
Last active December 28, 2015 11:08
Show Gist options
  • Save dpetersen/7490855 to your computer and use it in GitHub Desktop.
Save dpetersen/7490855 to your computer and use it in GitHub Desktop.
have_received vs. should_receive
class ConverterService
end
class CurrencyConverter
def initialize(yen)
@yen = yen
end
def to_human_dollars
dollars = ConverterService.yen_to_dollars(@yen)
rounded_dollars = dollars.round
"$#{rounded_dollars}"
end
end
require_relative 'currency_converter'
describe CurrencyConverter do
let(:converter) { CurrencyConverter.new(10_000) }
describe "#to_human_dollars" do
subject(:human_dollars) { converter.to_human_dollars }
before { ConverterService.stub(yen_to_dollars: 100.10) }
describe "with have_received" do
before { human_dollars }
it "calls the conversion service with the correct yen" do
expect(ConverterService).to have_received(:yen_to_dollars).with(10_000)
end
it { should eq("$100") }
end
describe "with should_receive" do
it "calls the conversion service with the correct yen" do
ConverterService.should_receive(:yen_to_dollars).with(10_000)
human_dollars
end
it { should eq("$100") }
end
end
end
source 'https://rubygems.org'
gem 'rspec'
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.5)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.7)
rspec-expectations (2.14.4)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.4)
PLATFORMS
ruby
DEPENDENCIES
rspec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment