Introduction to TDD and RSpec

Introduction to TDD and RSpec

Welcome to your second lesson in this course dedicated to practicing Test Driven Development (TDD) utilizing Ruby and RSpec. In this unit, we will continue adding features to our calculate_discount function. We have five more requirements for you to implement!

In this course, the emphasis is placed on hands-on practice, where you'll receive requirements through tests, one at a time. Your objective is to implement code that makes each test pass, simulating a real-world TDD environment. As the course guide, it's akin to being your pair programmer, providing test-driven prompts to hone your skills as you progress.

Understanding the Red-Green-Refactor Cycle

As a reminder, the Red-Green-Refactor cycle is central to TDD, guiding your development process:

  • Red: Start by writing a failing test to define the next step.
  • Green: Implement just enough code to pass the test, focusing on functionality.
  • Refactor: Optimize the code for clarity and efficiency without changing its behavior.

More Requirements for `calculate_discount` Function

In this section, you will enhance the calculate_discount function by implementing five additional requirements:

  1. Non-Numeric Price Input Handling: Learn how to validate inputs and handle errors for non-numeric price values.

  2. Non-Numeric Discount Input Handling: Understand how to ensure discount inputs are numeric and manage invalid cases effectively.

  3. Handling Very Small Prices: Discover techniques to manage very small prices and ensure they adhere to a defined minimum value post-discount.

  4. Minimum Discount Percentage Application: Implement logic to apply a minimum discount percentage when provided with a lower percentage.

  5. Capping Maximum Discount Amount: Explore methods for capping the discount at a maximum dollar value, ensuring the discount does not exceed this cap.

Below is the calculate_discount function that you will enhance by implementing five additional requirements:

Ruby
def calculate_discount(original_price, discount_percentage)
  # Placeholder for initial implementation
  # This version assumes valid numeric inputs and just calculates the discount
  original_price - (original_price * (discount_percentage / 100.0))
end

7. Non-Numeric Price Input Handling

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