Building a Shopping Cart with TDD in Ruby

Introduction to the Shopping Cart Module

Welcome to your third unit for this course dedicated to practicing Test Driven Development (TDD) utilizing Ruby and RSpec. In this lesson, we will explore the process of building a ShoppingCart class with multiple features using TDD.

The aim is to provide you with an understanding of how requirements are translated into tests, which are tackled one at a time. You'll observe how tests are written and code is implemented to pass each test, simulating a real-world TDD environment. Previously, tests were provided, but this time, the focus will be on creating them independently.

Remember to use the core concepts of the Red-Green-Refactor cycle while practicing these exercises. I'm still here to help! Just ask if you encounter any questions or issues.

Requirements for `ShoppingCart` Class

In this section, you will learn how to build the ShoppingCart class by implementing the following features:

  1. Starting with an Empty Cart
  2. Adding a Single Item
  3. Adding Multiple Items
  4. Handling Multiple Quantities of the Same Item
  5. Removing an Item

1. Starting with an Empty Cart

  • Description: When a new shopping cart is created, it should start without any items, and the total price should be zero.
  • Details:
    • Initialize a new shopping cart using the ShoppingCart constructor.
    • Ensure the item_count method returns 0 for an empty cart.
    • Verify the total method returns 0 for the initial state.
  • Examples: A newly created cart should have a count of 0 and a total of 0.
class ShoppingCart
  attr_reader :items # Allow access to items for external methods

  def initialize
    @items = [] # Initialize an empty cart
  end

  def item_count
    @items.sum { |item| item[:quantity] } # Calculate the total item count
  end

  def total
    @items.sum { |item| item[:price] * item[:quantity] } # Calculate the total price
  end
end

# Example Usage
cart = ShoppingCart.new
puts cart.item_count # Output: 0
puts cart.total      # Output: 0

2. Adding a Single Item

  • Description: Verify that when a single item is added to the cart, the cart's item count increases, and the total price reflects the added item's price.
  • Details:
    • Use the add_item(item) method to add an item to the cart.
    • Confirm that the item_count method returns the correct number of items after an item is added.
    • Ensure the total method accurately calculates and returns the total price of items in the cart.
  • Examples: Adding an item {'id' => '1', 'name' => 'Book', 'price' => 10} should result in an item count of 1 and a total cost of 10.
class ShoppingCart
  attr_reader :items

  def initialize
    @items = []
  end

  def item_count
    @items.sum { |item| item[:quantity] }
  end

  def total
    @items.sum { |item| item[:price] * item[:quantity] }
  end

  def add_item(item) # New method to add a single item
    @items << item.merge(quantity: 1) # Add item with a default quantity of 1
  end
end

# Example Usage
cart = ShoppingCart.new
cart.add_item({ id: '1', name: 'Book', price: 10 }) # Add a single item to the cart
puts cart.item_count # Output: 1
puts cart.total      # Output: 10
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal