Skip to content

Instantly share code, notes, and snippets.

@thimo
Created March 31, 2025 11:37
Show Gist options
  • Select an option

  • Save thimo/e96a6d2d70163563bace87a45291b157 to your computer and use it in GitHub Desktop.

Select an option

Save thimo/e96a6d2d70163563bace87a45291b157 to your computer and use it in GitHub Desktop.
cart_calculator_spec.rb
require_relative "cart_calculator"
RSpec.describe CartCalculator do
let(:products) do
[
{ name: "Book", price: 20 },
{ name: "Pen", price: 5 },
{ name: "Notebook", price: 15 }
]
end
describe "#calculate_total" do
it "calculates basic total without discounts" do
calculator = CartCalculator.new(products)
result = calculator.calculate_total
expect(result[:subtotal]).to eq(40)
expect(result[:discount]).to eq(0)
expect(result[:shipping]).to eq(10)
expect(result[:total]).to eq(50)
end
it "applies free shipping for orders over $50" do
expensive_products = [
{ name: "Premium Book", price: 60 }
]
calculator = CartCalculator.new(expensive_products)
result = calculator.calculate_total
expect(result[:shipping]).to eq(0)
end
it "applies bulk discount for orders over $100" do
bulk_products = [
{ name: "Premium Book", price: 120 }
]
calculator = CartCalculator.new(bulk_products)
result = calculator.calculate_total
expect(result[:discount]).to eq(12) # 10% of 120
expect(result[:total]).to eq(108) # 120 - 12 (no shipping, over $50)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment