Last active
December 28, 2015 11:08
-
-
Save dpetersen/7490855 to your computer and use it in GitHub Desktop.
have_received vs. should_receive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--color |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'rspec' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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