Skip to content

Instantly share code, notes, and snippets.

@jjb
Created March 12, 2013 03:47
Show Gist options
  • Save jjb/5140160 to your computer and use it in GitHub Desktop.
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
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
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