Ejemplo n.º 1
0
        public void Calculate_Cart_List()
        {
            Product p1 = new Product { Name = "P1", ProductID = 1 , Price = 100M};
            Product p2 = new Product { Name = "P2", ProductID = 2, Price = 50M };

            CartRepository target = new CartRepository();

            target.AddItem(p1, 2);
            target.AddItem(p2, 3);

            var result = target.ComputeTotalValue();

            Assert.AreEqual(result, new decimal(100 * 2 + 50 * 3));
        }
Ejemplo n.º 2
0
 public RedirectToRouteResult AddToCart(CartRepository cart,int productId, string returnUrl)
 {
     Product product = _repo.Products.SingleOrDefault(p => p.ProductID == productId);
     if (product != default(Product))
         cart.AddItem(product, 1);
     return RedirectToAction("Index", new { returnUrl });
 }
Ejemplo n.º 3
0
        public void Cannot_Checkout_Invalid_ShippingDetails()
        {
            // przygotowanie - tworzenie imitacji procesora zamówień
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
            // przygotowanie - tworzenie koszyka z produktem
            CartRepository cart = new CartRepository();
            cart.AddItem(new Product(), 1);

            // przygotowanie - tworzenie egzemplarza kontrolera
            CartController target = new CartController(null, mock.Object);
            // przygotowanie - dodanie błędu do modelu
            target.ModelState.AddModelError("error", "error");

            // działanie - próba zakończenia zamówienia
            ViewResult result = target.CheckOut(cart, new ShippingDetails());

            // asercje - sprawdzenie, czy zamówienie zostało przekazane do procesora
            mock.Verify(m => m.ProcessOrder(It.IsAny<CartRepository>(), It.IsAny<ShippingDetails>()),
                Times.Never());
            // asercje - sprawdzenie, czy metoda zwraca domyślny widok
            Assert.AreEqual("", result.ViewName);
            // asercje - sprawdzenie, czy przekazujemy prawidłowy model do widoku
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
Ejemplo n.º 4
0
        public void Can_Remove_Lines()
        {
            Product p1 = new Product { Name = "P1", ProductID = 1 };
            Product p2 = new Product { Name = "P2", ProductID = 2 };
            Product p3 = new Product { Name = "P3", ProductID = 3 };

            CartRepository target = new CartRepository();

            target.AddItem(p1, 1);
            target.AddItem(p2, 2);
            target.AddItem(p1, 10);
            target.AddItem(p3, 5);

            var results = target.CartLineCollections;

            Assert.AreEqual(results.Count, 3);
            Assert.AreEqual(results[0].Product, p1);
            Assert.AreEqual(results[1].Product, p2);
            Assert.AreEqual(results[2].Product, p3);

            target.RemoveLine(p2);

            results = target.CartLineCollections;

            Assert.AreEqual(results.Count, 2);
            Assert.AreEqual(results[0].Product, p1);
            Assert.AreEqual(results[1].Product, p3);
            Assert.IsFalse(results.Any(p => p.Product.ProductID == p2.ProductID));
        }
Ejemplo n.º 5
0
        public void Can_Clear_Contents()
        {
            Product p1 = new Product { Name = "P1", ProductID = 1 };
            Product p2 = new Product { Name = "P2", ProductID = 2 };

            CartRepository target = new CartRepository();

            target.AddItem(p1, 1);
            target.AddItem(p2, 1);

            var results = target.CartLineCollections.ToArray();

            Assert.AreEqual(results.Length, 2);

            target.Clear();
            Assert.AreEqual(target.CartLineCollections.Count, 0);
        }
Ejemplo n.º 6
0
        public void Can_Add_Quantity_For_Existing_Lines()
        {
            Product p1 = new Product { Name = "P1", ProductID = 1 };
            Product p2 = new Product { Name = "P2", ProductID = 2 };

            CartRepository target = new CartRepository();

            target.AddItem(p1, 1);
            target.AddItem(p2, 2);
            target.AddItem(p1, 10);

            var results = target.CartLineCollections;

            Assert.AreEqual(results.Count, 2);
            Assert.AreEqual(results[0].Quantity, 11);
            Assert.AreEqual(results[1].Quantity, 2);
        }
Ejemplo n.º 7
0
        public void Can_Add_New_Lines()
        {
            Product p1 = new Product { Name = "P1", ProductID = 1 };
            Product p2 = new Product {Name = "P2", ProductID = 2 };

            CartRepository target = new CartRepository();

            target.AddItem(p1, 1);
            target.AddItem(p2, 1);

            var results = target.CartLineCollections;

            Assert.AreEqual(results.Count, 2);
            Assert.AreEqual(results[0].Product, p1);
            Assert.AreEqual(results[1].Product, p2);
        }