AgreeleQueert
New Member
In my web store app I have a "Cart" class that can add, remove, and compute total value of items. The data model is the folowing: 1 Item contains 1 Product and 1 Shipping. Product has Price field and Shipping has Cost field. Here is the Cart class code: \[code\]public class CartLine{ public Item Item { get; set; } public int Quantity { get; set; }}public class Cart{ private List<CartLine> lineCollection = new List<CartLine>(); // methods: // Add(Item item, int quantity) // Remove(Item item) public decimal ComputeTotalProductValue() { return lineCollection.Sum(l => l.Item.Product.Price*l.Quantity); } // methods with the same principle: // ComputeTotalShippingValue() // ComputeOveralValue()}\[/code\]And here is my unit test (that of course doesn't work): \[code\][TestMethod] public void Can_Compute_Total_Values() { // Arrange var itemMock = new Mock<IItemsRepository>(); itemMock.Setup(i => i.GetItems()).Returns(new[] { new Item { ItemId = 1, ProductId = 1, ShippingId = 1 }, new Item { ItemId = 2, ProductId = 2, ShippingId = 2 } }); var productMock = new Mock<IProductRepository>(); productMock.Setup(p => p.GetProducts()).Returns(new[] { new Product { ProductId = 1, Price = 10 }, new Product { ProductId = 2, Price = 20 } }); var shippingMock = new Mock<IShippingRepository>(); shippingMock.Setup(s => s.GetShippings()).Returns(new[] { new Shipping { ShippingId = 1, Cost = 2 }, new Shipping { ShippingId = 2, Cost = 5 } }); var item1 = itemMock.Object.GetItems().ToArray()[0]; var item2 = itemMock.Object.GetItems().ToArray()[1]; var target = new Cart(); //Act target.Add(item1, 2); target.Add(item2, 4); decimal totalProduct = target.ComputeTotalProductValue(); decimal totalShipping = target.ComputeTotalShippingValue(); decimal overalSum = target.ComputeOveralValue(); // Assert Assert.AreEqual(totalProduct, 100); Assert.AreEqual(totalShipping, 24); Assert.AreEqual(overalSum, 124); }}\[/code\]The issue is probably associated with binding Items to Products and Shipping. How can I do this?Thanks in advance!