I am fairly new to writing unit tests, and I was wondering if this approach I have taken is on the right track?
Given the following interfaces/classes:
public interface ILoggingService
{
void Log(string message);
}
public interface IShoppingCart
{
IList<IShoppingCartItem> Items { get; }
}
public interface IShoppingCartItem
{
string Sku { get; set; }
string Name { get; set; }
string Description { get; set; }
decimal Price { get; set; }
int Quantity { get; set; }
}
public interface IShippingCalculator
{
decimal Calculate();
}
public class ShoppingCartItem : IShoppingCartItem
{
public string Sku { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
}
public class ShippingCalculator : IShippingCalculator
{
private readonly ILoggingService loggingService;
private readonly IShoppingCart shoppingCart;
public ShippingCalculator(IShoppingCart shoppingCart, ILoggingService loggingService)
{
this.shoppingCart = shoppingCart;
this.loggingService = loggingService;
}
public decimal Calculate()
{
if (shoppingCart.Items.Count == 0)
return 0;
var pricePerItem = 3;
return shoppingCart.Items.Sum(x => x.Quantity * pricePerItem);
}
}
Given the requirements of:
- The Shipping Calculator must calculate shipping at the rate of $3.00 per item.
Does this unit test look correct?
public class ShippingCalculatorTests
{
[Fact]
public void Test1()
{
//Arrange
var cart = new Mock<IShoppingCart>();
cart.Setup(m => m.Items).Returns(
new List<IShoppingCartItem>() {
new ShoppingCartItem() { Quantity = 5 },
new ShoppingCartItem() { Quantity = 3 }
}
);
var logger = new Mock<ILoggingService>();
var calc = new ShippingCalculator(cart.Object, logger.Object);
//Act
var result = calc.Calculate();
//Assert
Assert.Equal(24, result);
}
}
Any advice/recommendations would be greatly appreciated.