> For the complete documentation index, see [llms.txt](https://raviram.gitbook.io/tdd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://raviram.gitbook.io/tdd/tdd-exercises-2/shopping-cart/test-add-cart-item/test-assert-an-exception.md).

# Test: Assert an Exception

Write and test a validation feature in the ShoppingCart class to prevent the addition of items with negative quantities or prices.

**Part 1: Writing the Failing Test**

1. **Create the Test for Negative Quantity**

   * In your test project, under `ShoppingCartTests.cs`, write a new test method to ensure that an exception is thrown when attempting to add an item with a negative quantity.

   ```csharp
   [Fact]
   public void AddItem_WithNegativeQuantity_ThrowsArgumentException()
   {
       var cart = new ShoppingCart();
       var item = new Item("Apple", -1, 0.75);

       Action act = () => cart.AddItem(item);

       act.Should().Throw<ArgumentException>().WithMessage("*negative*");
   }
   ```
2. **Run the Test**
   * Execute the test. It should fail, indicating that the ShoppingCart class currently does not handle this scenario.

**Part 2: Implementing the Validation**

3. **Add Validation Logic to ShoppingCart**

   * Modify the `AddItem` method in the `ShoppingCart` class to include validation logic that checks for negative quantities and prices.

   ```csharp
   public class ShoppingCart
   {
       // ... existing code ...

       public void AddItem(Item item)
       {
           if (item.Quantity < 0 || item.Price < 0)
           {
               throw new ArgumentException("Quantity and price must be non-negative.");
           }
           Items.Add(item);
       }
   }
   ```
4. **Re-run the Test**
   * Run the test again. It should now pass, confirming that the ShoppingCart class correctly handles negative quantities.

**Part 3: Refactoring (Optional)**

5. **Refactor if Necessary**
   * Review the code and tests for any possible improvements in readability or efficiency.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://raviram.gitbook.io/tdd/tdd-exercises-2/shopping-cart/test-add-cart-item/test-assert-an-exception.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
