Welcome to your first lesson in this course dedicated to practicing Test Driven Development (TDD) utilizing a C# and xUnit. Test Driven Development is an effective approach that emphasizes writing tests before coding. This process helps you develop your application around testing, ensuring each component functions as intended. In this lesson, you'll learn the fundamentals of TDD along with the Red-Green-Refactor cycle, understanding their roles in creating consistent and maintainable code.
This course emphasizes hands-on practice, where you'll receive requirements through tests, one at a time. Your task is to implement code that makes each test pass, simulating a real-world TDD environment. Consider this course guide as your pair programmer, providing test-driven prompts to sharpen your skills as you progress.
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.
- Description: The function must correctly apply a percentage-based discount to an original price.
- Test Case:
C#
1 [Fact] 2 public void CalculateDiscount_AppliesCorrectDiscount() 3 { 4 // Arrange 5 var math = new Math(); 6 double originalPrice = 100; 7 double discountPercentage = 20; 8 double expectedDiscountedPrice = 80; 9 10 // Act 11 var result = math.CalculateDiscount(originalPrice, discountPercentage); 12 13 // Assert 14 Assert.Equal(expectedDiscountedPrice, result, 1); 15 }
- Description: The function should return the original price when the discount percentage is 0.
- Test Case:
C#1 [Fact] 2 public void CalculateDiscount_ReturnsOriginalPriceWhenDiscountIsZero() 3 { 4 // Arrange 5 var math = new Math(); 6 double originalPrice = 50; 7 double discountPercentage = 0; 8 9 // Act 10 var result = math.CalculateDiscount(originalPrice, discountPercentage); 11 12 // Assert 13 Assert.Equal(originalPrice, result); 14 }
- Description: The function must accurately handle decimal percentages in discount calculations and round the result to two decimal places.
- Test Case:
C#
1 [Fact] 2 public void CalculateDiscount_HandlesDecimalDiscountsCorrectly() 3 { 4 // Arrange 5 var math = new Math(); 6 double originalPrice = 100; 7 double discountPercentage = 33.333; 8 double expectedDiscountedPrice = 66.67; 9 10 // Act 11 var result = math.CalculateDiscount(originalPrice, discountPercentage); 12 13 // Assert 14 Assert.Equal(expectedDiscountedPrice, result); 15 }
- Description: The function should not accept negative prices and must throw an error if encountered.
- Test Case:
C#1 [Fact] 2 public void CalculateDiscount_ShouldThrowErrorForNegativePrices() 3 { 4 // Arrange 5 var math = new Math(); 6 double originalPrice = -50; 7 double discountPercentage = 10; 8 9 // Act, Assert 10 Assert.Throws<ArgumentException>(() => math.CalculateDiscount(originalPrice, discountPercentage)); 11 }
- Description: The function should not accept discount percentages greater than 100% and must throw an error in such cases.
- Test Case:
C#
1 [Fact] 2 public void CalculateDiscount_ThrowsErrorForDiscountsGreaterThan100Percent() 3 { 4 // Arrange 5 var math = new Math(); 6 var originalPrice = 100; 7 var discountPercentage = 110; 8 9 // Act & Assert 10 Assert.Throws<System.ArgumentException>(() => math.CalculateDiscount(originalPrice, discountPercentage)); 11 }
Looking ahead to the practice exercises, you will have the opportunity to ensure all tests pass while practicing the Red-Green-Refactor cycle. Your implementation may differ from the provided solutions, and that’s perfectly acceptable. Each practice session will begin from a solution foundation, allowing you to compare your approach with the guided solution and develop your features to ensure test success.
As you undertake these exercises, remember to engage in the Red-Green-Refactor cycle. Each test I provide serves as the "Red" phase, marking the next step to achieve. Your task is to transition through the "Green" phase by making the tests pass and then enter the "Refactor" phase to enhance your code's clarity and maintainability while confirming that all tests remain successful.