Skip to content

Instantly share code, notes, and snippets.

@joebew42
Created April 5, 2013 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joebew42/5319780 to your computer and use it in GitHub Desktop.
Save joebew42/5319780 to your computer and use it in GitHub Desktop.
require File.join(File.dirname(__FILE__), 'refactoring_example.rb')
describe Price do
let(:prices) do
{
:regular => RegularPrice.new,
:new_release => NewReleasePrice.new,
:children => ChildrenPrice.new,
}
end
context "when create a new 'regular price'" do
it "is an instance of RegualarPrice" do
prices[:regular].should be_an_instance_of(RegularPrice)
end
end
context "when create a new 'new release price'" do
it "is an instance of NewReleasePrice" do
prices[:new_release].should be_an_instance_of(NewReleasePrice)
end
end
context "when create a new 'children price'" do
it "is an instance of ChildrenPrice" do
prices[:children].should be_an_instance_of(ChildrenPrice)
end
end
context "when calls get_charge() and the price has a defined block to call" do
it "returns 3.5 on 'regular' price, after 3 days rental" do
prices[:regular].get_charge(3).should == 3.5
end
it "returns 9 on 'new_release' price, after 3 days rental" do
prices[:new_release].get_charge(3).should == 9
end
it "returns 1.5 on 'regular' price, after 3 days rental" do
prices[:children].get_charge(3).should == 1.5
end
end
end
describe Movie do
let(:movie) { Movie.new 'movie title', Movie::REGULAR }
context "when create a new movie" do
it "is an instance of Movie" do
movie.should be_an_instance_of(Movie)
end
it "maintains correct informations" do
movie.title.should == 'movie title'
movie.price_code == Movie::REGULAR
end
end
context "when calculate the charge" do
it "returns 5 for a regular movie rented for 4 days" do
movie.get_charge(4).should == 5
end
end
end
describe Rental do
let(:movies) do
{
:regular => Movie.new('regular movie', Movie::REGULAR),
:new_release => Movie.new('new_release movie', Movie::NEW_RELEASE),
:children => Movie.new('children movie', Movie::CHILDREN),
}
end
let(:rentals) do
{
:regular => Rental.new(movies[:regular], 4),
:new_release => Rental.new(movies[:new_release], 6),
:children => Rental.new(movies[:children], 8),
}
end
context "when makes a new rental" do
it "is an instance of Rental" do
rentals.each { |key,rental| rental.should be_an_instance_of(Rental) }
end
it "maintains correct informations" do
days_rented = 4
rentals.each do |key, rental|
rentals[key].movie.should be_eql(movies[key])
rentals[key].days_rented.should == days_rented
days_rented += 2
end
end
end
context "when calculate the charge" do
it "returns 5 for a regular movie rented for 4 days" do
rentals[:regular].get_charge.should == 5
end
it "returns 18 for a new release movie rented for 6 days" do
rentals[:new_release].get_charge.should == 18
end
it "returns 9 for a children movie rented for 8 days" do
rentals[:children].get_charge.should == 9
end
end
end
describe Customer do
let(:customer) { Customer.new('John') }
let(:movies) do
{
:regular => Movie.new('regular movie', Movie::REGULAR),
:new_release => Movie.new('new_release movie', Movie::NEW_RELEASE),
:children => Movie.new('children movie', Movie::CHILDREN),
}
end
let(:rentals) do
{
:regular => Rental.new(movies[:regular], 4),
:new_release => Rental.new(movies[:new_release], 6),
:children => Rental.new(movies[:children], 8),
}
end
context "when create a new customer" do
it "is an instance of Customer" do
customer.should be_an_instance_of(Customer)
end
it "maintains correct informations" do
customer.name.should == 'John'
end
end
context "when calculate the total amount of rental's charge" do
it "returns 5 with only one 'regular' rental of 4 days" do
customer.add_rental rentals[:regular]
customer.get_total_charge.should == 5
end
it "returns 23 with another 'new_release' rental of 6 days" do
customer.add_rental rentals[:regular]
customer.add_rental rentals[:new_release]
customer.get_total_charge.should == 23
end
it "returns 32 with another 'children' rental of 8 days" do
customer.add_rental rentals[:regular]
customer.add_rental rentals[:new_release]
customer.add_rental rentals[:children]
customer.get_total_charge.should == 32
end
end
context "when calculate the total frequent renter points" do
it "returns 1 with only one 'regular' rental of 4 days" do
customer.add_rental rentals[:regular]
customer.get_total_frequent_renter_points.should == 1
end
it "returns 3 with another 'new_release' rental of 6 days" do
customer.add_rental rentals[:regular]
customer.add_rental rentals[:new_release]
customer.get_total_frequent_renter_points.should == 3
end
it "returns 4 with another 'children' rental of 8 days" do
customer.add_rental rentals[:regular]
customer.add_rental rentals[:new_release]
customer.add_rental rentals[:children]
customer.get_total_frequent_renter_points.should == 4
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment