DotNet Core C# TDD Part2

Amir Hoss
3 min readAug 22, 2023

--

<< Part I create the first test case

Let's add other tests

For the first scenario, we can modify the function name to “ShouldReturnMultipleArticlesAddedToCart” to indicate that we are testing the functionality of adding multiple articles to the cart.

For the second scenario, we can modify the function name to “ShouldReturnCombinedArticlesAddedToCart” to indicate that we are testing the functionality of adding the same article multiple times to the cart.

// ProjectTDD.Core.Tests/ShoppingCartTest.cs

using System;
namespace ProjectTDD.Core
{
public class ShoppingCartTest
{
[SetUp]
public void SetUp()
{

}

[Test]
public void ShouldReturnArticleAddedToCart()
{
// Arrange:
// Prepare the necessary objects and environment for the test.

var item = new AddToCartItem()
{
ArticleId = 7742,
Quantity = 2
};

var request = new AddToCartRequest()
{
Item = item
};

var manager = new ShoppingCartManager();

// Act:
// Perform the action that you want to test.
AddToCartResponse response = manager.AddToCart(request);


// Assert:
// Verify the outcome of the action, making sure it meets your expectations.
Assert.NotNull(response);
Assert.Contains(item, response.Items);
}

[Test]
public void ShouldReturnArticlesAddedToCart()
{
var item1 = new AddToCartItem(){
ArticleId = 8842,
Quantity = 8
};

var request = new AddToCartRequest(){
Item = item1
};

var manager = new ShoppingCartManager();

AddToCartResponse response = manager.AddToCart(request);

var item2 = new AddToCartItem()
{
ArticleId = 7742,
Quantity = 5
};
request = new AddToCartRequest()
{
Item = item2
};

response = manager.AddToCart(request);

Assert.NotNull(response);
Assert.Contains(item1, response.Items);
Assert.Contains(item2, response.Items);
}

[Test]
public void ShouldReturnCombinedArticlesAddedToCart()
{
var item1 = new AddToCartItem()
{
ArticleId = 7742,
Quantity = 2
};
var request = new AddToCartRequest()
{
Item = item1
};
var manager = new ShoppingCartManager();

AddToCartResponse response = manager.AddToCart(request);

var item2 = new AddToCartItem()
{
ArticleId = 7742,
Quantity = 5
};
request = new AddToCartRequest()
{
Item = item2
};
response = manager.AddToCart(request);


Assert.NotNull(response);
Assert.That(Array.Exists(response.Items, item => item.ArticleId == 7742 && item.Quantity == 7));

}
}
}

Now fix code

If run “dotnet test” we have an error, so we have to check and fix the it

For the first scenario, I change this

//ProjectTDD.Core.Tests/ShoppingCartManager.cs

internal class ShoppingCartManager
{
private List<AddToCartItem> _shoppingCart;

public ShoppingCartManager()
{
_shoppingCart = new List<AddToCartItem>();
}


internal AddToCartResponse AddToCart(AddToCartRequest request)
{
_shoppingCart.Add(request.Item);

return new AddToCartResponse()
{
Items = _shoppingCart.ToArray()
};

}
}

And for the second scenario, I need to change this again to fix it

    internal AddToCartResponse AddToCart(AddToCartRequest request)
{
var item = _shoppingCart.Find(i => i.ArticleId == request.Item.ArticleId);
if (item != null)
{
item.Quantity += request.Item.Quantity;
}
else
{
_shoppingCart.Add(request.Item);
}

return new AddToCartResponse()
{
Items = _shoppingCart.ToArray()
};
}

Final Code

//ProjectTDD.Core.Tests/ShoppingCartManager.cs
using System;

namespace ProjectTDD.Core
{
internal class ShoppingCartManager
{
private List<AddToCartItem> _shoppingCart;

public ShoppingCartManager()
{
_shoppingCart = new List<AddToCartItem>();
}


internal AddToCartResponse AddToCart(AddToCartRequest request)
{
var item = _shoppingCart.Find(i => i.ArticleId == request.Item.ArticleId);
if (item != null)
{
item.Quantity += request.Item.Quantity;
}
else
{
_shoppingCart.Add(request.Item);
}

return new AddToCartResponse()
{
Items = _shoppingCart.ToArray()
};
}
}
}

Test with the menu or Command Console

- Run test in View>Tests
> dotent test in console

<< Part I create the first test case

--

--

Amir Hoss

✔Senior PHP Developer✔Mobile Developer✔MySQL Administrator [myWebsite:https://amirhome.com]