Created
March 12, 2013 03:47
-
-
Save jjb/5140160 to your computer and use it in GitHub Desktop.
Demonstration of why private methods are problematic for specing, and also potentially a design smell, for discusion on rubyparley
This file contains hidden or 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 Product | |
attr_accessible :price | |
attr_accessible :name | |
def long_listing | |
"The price of #{name} is #{human_price}" | |
end | |
def short_listing | |
"#{name}: #{human_price}" | |
end | |
private | |
def human_price | |
if price.nil? | |
p = 0 | |
else | |
p = price | |
end | |
number_to_currency(p, unit: '$') | |
end | |
end |
This file contains hidden or 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
describe Product | |
let(:product) = Product.new | |
describe "listings" do | |
before do | |
product.stub(:name){"The Name"} | |
product.stub(:human_price){"$1.23"} | |
end | |
describe "#long_listing" do | |
specify{product.long_listing.should eq("The price of The Name is $1.23")} | |
end | |
describe "#short_listing" do | |
specify{product.short_listing.should eq("The Name: $1.23")} | |
end | |
end | |
## Can't actually do this spec, because the method is private! | |
describe "#human_price" do | |
subject{product.human_price} | |
context "when the price is nil" do | |
it{should eq "$0.00" } | |
end | |
context "when the price is present" do | |
before{product.stub(:price){1.23}} | |
it{should eq "$1.23" } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment