Introduction to Practice

Welcome to your first lesson dedicated to practicing Test Driven Development (TDD) using Scala 3 and ScalaTest. Test Driven Development is an effective methodology that emphasizes writing tests before developing code. This approach assists in designing your application with a focus on testing as a crucial element, ensuring each component performs as expected. In this lesson, you'll grasp the fundamentals of TDD along with the Red-Green-Refactor cycle, understanding their significance in creating reliable and maintainable code.

This course involves hands-on practice, where you'll encounter requirements incrementally through tests. Your task is to implement code that satisfies each test, mirroring a real-world TDD scenario. Consider this course guide as your pair programmer, offering test-driven prompts to enhance your proficiency 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: Begin by writing a failing test to establish the next goal.
  • Green: Develop just enough code to pass the test, emphasizing functionality.
  • Refactor: Improve the code for clarity and efficiency without altering its behavior.
Requirements for `calculateDiscount` Function

In this lesson, we'll be using AnyFunSpec from ScalaTest to define and execute our tests. Below, you'll find the detailed requirements and corresponding test cases for the calculateDiscount function. Each requirement directs you through the TDD process, focusing on specific behaviors the function must demonstrate.

1. Correct Discount Application
  • Description: The function must correctly apply a percentage-based discount to an original price.
  • Test Case:

The test confirms that the function applies the discount correctly and allows for small floating-point deviations.

2. Zero Discount Handling
  • Description: The function should return the original price when the discount percentage is 0.
  • Test Case:

This test checks that with a 0% discount, the function should correctly maintain the original price.

3. Decimal Discount Precision
  • Description: The function must accurately handle decimal percentages in discount calculations and round the result to two decimal places.
  • Test Case:

It tests the function's ability to handle decimal discounts and verify rounding to two decimal points for accuracy.

4. Negative Price Handling
  • Description: The function should not accept negative prices and must throw an error if encountered.
  • Test Case:

This test is critical for ensuring that the function rejects invalid negative price inputs by raising an exception.

5. Discount Percentage Greater Than 100%
  • Description: The function should not accept discount percentages greater than 100% and must throw an error in such scenarios.
  • Test Case:

It evaluates the method’s behavior when given excessive discount percentages that logically exceed the threshold.

Summary and Preparation for Practice

Looking ahead to the practice exercises, you'll have the chance to ensure all tests pass while practicing the Red-Green-Refactor cycle using Scala 3 and ScalaTest. Your implementation might differ from the provided solutions, and that's entirely acceptable. Each practice session will start from a solution foundation, allowing you to compare your approach with the guided solution and develop your features to ensure test success.

As you engage in these exercises, remember to follow the Red-Green-Refactor cycle. Each test I provide serves as the "Red" phase, marking the next step to achieve. Your challenge is to transition through the "Green" phase by making the tests pass and then move to the "Refactor" phase to enhance your code's clarity and maintainability while confirming that all tests remain successful.

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