public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl)
 {
     Product product = repository.Products.FirstOrDefault(p => p.ProductID == productId);
     if (product != null)
     {
         cart.AddItem(product, 1);
     }
     return RedirectToAction("Index", new { returnUrl });
 }
Esempio n. 2
0
        public void Can_Add_New_Lines()
        {
            // Arrange
            Product p1 = new Product { ProductID = 1, Name = "P1" };
            Product p2 = new Product { ProductID = 2, Name = "P2" };

            // Arrange
            Cart target = new Cart();

            // Act
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            CartLine[] results = target.Lines.ToArray();

            // Assert
            Assert.AreEqual(results.Length, 2);
            Assert.AreEqual(results[0].Product, p1);
            Assert.AreEqual(results[1].Product, p2);
        }
Esempio n. 3
0
        public void Can_Add_Quantity_For_Existing_Lines()
        {
            // Arrange
            Product p1 = new Product { ProductID = 1, Name = "P1" };
            Product p2 = new Product { ProductID = 2, Name = "P2" };

            // Arrange
            Cart target = new Cart();

            // Act
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);
            target.AddItem(p1, 10);
            CartLine[] results = target.Lines.OrderBy(c => c.Product.ProductID).ToArray();

            // Assert
            Assert.AreEqual(results.Length, 2);
            Assert.AreEqual(results[0].Quantity, 11);
            Assert.AreEqual(results[1].Quantity, 1);
        }
Esempio n. 4
0
        public void Can_Remove_Line()
        {
            // Arrange
            Product p1 = new Product { ProductID = 1, Name = "P1" };
            Product p2 = new Product { ProductID = 2, Name = "P2" };
            Product p3 = new Product { ProductID = 3, Name = "P3" };

            // Arrange
            Cart target = new Cart();

            // Arrange
            target.AddItem(p1, 1);
            target.AddItem(p2, 3);
            target.AddItem(p3, 5);
            target.AddItem(p2, 1);

            // Act
            target.RemoveLine(p2);

            // Assert
            Assert.AreEqual(target.Lines.Where(c => c.Product == p2).Count(), 0);
            Assert.AreEqual(target.Lines.Count(), 2);
        }
Esempio n. 5
0
 public void Calculate_Cart_Total()
 {
     // Arrange
     Product p1 = new Product { ProductID = 1, Name = "P1", Price = 100M };
     Product p2 = new Product { ProductID = 2, Name = "P2" , Price = 50M};
     
     // Arrange
     Cart target = new Cart();
     
     // Act
     target.AddItem(p1, 1);
     target.AddItem(p2, 1);
     target.AddItem(p1, 3);
     decimal result = target.CalculateTotalValue();
     
     // Assert
     Assert.AreEqual(result, 450M);
 
 }
Esempio n. 6
0
        public void Can_Checkout_And_Submit_Order() {

            // Arrange
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();

            // Arrange
            Cart cart = new Cart();
            
            cart.AddItem(new Product(), 1);
            
            // Arrange
            CartController target = new CartController(null, mock.Object);

            // Act
            ViewResult result = target.Checkout(cart, new ShippingDetails());
            
            // Assert
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(),
                It.IsAny<ShippingDetails>()),
                Times.Once());

            // Assert
            Assert.AreEqual("Completed", result.ViewName);

            // Assert
            Assert.AreEqual(true, result.ViewData.ModelState.IsValid);
        }
Esempio n. 7
0
        public void Cannot_Checkout_Invalid_ShippingDetails() {

            // Arrange
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
            // Arrange
            Cart cart = new Cart();
            cart.AddItem(new Product(), 1);

            // Arrange
            CartController target = new CartController(null, mock.Object);

            // Arrange
            target.ModelState.AddModelError("error", "error");

            // Act
            ViewResult result = target.Checkout(cart, new ShippingDetails());

            // Assert
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(),
                It.IsAny<ShippingDetails>()),
                Times.Never());

            // Assert
            Assert.AreEqual("", result.ViewName);

            // Assert
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
Esempio n. 8
0
        public void Can_Clear_Contents()
        {
            // Arrange
            Product p1 = new Product
            {
                ProductID = 1,
                Name = "P1",
                Price = 100M
            };
            Product p2 = new Product { ProductID = 2, Name = "P2", Price = 50M };

            // Arrange
            Cart target = new Cart();
            
            // Arrange
            target.AddItem(p1, 1);
            target.AddItem(p2, 1);

            // Act
            target.Clear();

            // Assert
            Assert.AreEqual(target.Lines.Count(), 0);
        }