# Test: Calculate Total

Implement and test the functionality to calculate the total cost of items in a shopping cart.

**1. Write the Failing Test**

* Test the 'CalculateTotalCost' functionality by adding items to the cart and asserting the expected total cost.

```csharp
[Fact]
public void CalculateTotalCost()
{
    var cart = new ShoppingCart();
    cart.AddItem(new Item("Apple", 2, 0.75));
    cart.AddItem(new Item("Banana", 3, 0.50));

    var total = cart.CalculateTotalCost();

    total.Should().Be(2 * 0.75 + 3 * 0.50);
}
```

**2. Implement a Deliberately Failing Method**

* In the ShoppingCart class, implement the CalculateTotalCost method to return a wrong value initially.

```csharp
public class ShoppingCart
{
    public List<Item> Items { get; } = new List<Item>();

    public void AddItem(Item item)
    {
        Items.Add(item);
    }

    public double CalculateTotalCost()
    {
        return -1;
    }
}
```

**3. Run the Test and Confirm It Fails**

* The test should fail, validating its effectiveness.

**4. Implement the Correct Method to Pass the Test**

* Update the CalculateTotalCost method with the correct implementation.

```csharp
public double CalculateTotalCost()
{
    double total = 0.0;
    foreach (var item in Items)
    {
        total += item.Price * item.Quantity;
    }
    return total;
}
```

**5. Re-run the Test**

* The test should now pass, confirming the correct implementation.

**6. Refactor if Needed**

* Evaluate the code for possible refactoring.

This approach ensures that each aspect of the functionality is tested and verified, adhering to the TDD principles. The initial failing test (Step 2) confirms that the test is valid and only passes when the correct logic is implemented (Step 4).


---

# Agent Instructions: 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-calculate-total.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.
