Example #1
0
        public void Can_Remove_Product_From_Cart()
        {
            // Arrange: Set up a mock repository with two products
            var mockProductsRepos = new Moq.Mock<IProductsRepository>();
            var products = new System.Collections.Generic.List<Product> {
            new Product { ProductID = 14, Name = "Much Ado About Nothing" },
            new Product { ProductID = 27, Name = "The Comedy of Errors" },
            };
            mockProductsRepos.Setup(x => x.Products)
                         .Returns(products.AsQueryable());
            var cart = new Cart();
            cart.AddItem(products[1], 2); // 2x Comedy of Errors
            cart.AddItem(products[0], 3); // 3x Much Ado
            var controller = new CartController(mockProductsRepos.Object, null);

            // Act: Try removing Much Ado
            RedirectToRouteResult result =
            controller.RemoveFromCart(cart, 14, "someReturnUrl");

            // Assert
            Assert.AreEqual(1, cart.Lines.Count);
            Assert.AreEqual("The Comedy of Errors", cart.Lines[0].Product.Name);
            Assert.AreEqual(2, cart.Lines[0].Quantity);

            // Check that the visitor was redirected to the cart display screen
            Assert.AreEqual("Index", result.RouteValues["action"]);
            Assert.AreEqual("someReturnUrl", result.RouteValues["returnUrl"]);
        }
Example #2
0
 public void Calculates_Total_Value_Correctly()
 {
     Cart cart = new Cart();
     cart.AddItem(new Product { ProductID = 1, Price = 5 }, 10);
     cart.AddItem(new Product { ProductID = 2, Price = 2.1M }, 3);
     cart.AddItem(new Product { ProductID = 3, Price = 1000 }, 1);
     Assert.AreEqual(1056.3, cart.ComputeTotalValue());
 }
Example #3
0
        public void Can_Remove_Item() {
            Cart cart = new Cart();
            Product p1 = new Product() { ProductID = 1, Price = 5 };
            Product p2 = new Product() { ProductID = 2, Price = 15 };
            cart.AddItem(p1, 15);
            cart.AddItem(p2, 7);

            cart.RemoveLine(p1);

            Assert.AreEqual(1, cart.Lines.Count);
            Assert.AreEqual(2, cart.Lines[0].Product.ProductID);
            Assert.AreEqual(7, cart.Lines[0].Quantity);
        }
Example #4
0
 public RedirectToRouteResult AddToCart(Cart cart, int productID, string returnUrl)
 {
     Product product = productsRepository.Products
                         .FirstOrDefault(p => p.ProductID == productID);
     cart.AddItem(product, 1);
     return RedirectToAction("Index", new { returnUrl });
 }
Example #5
0
 public void Can_Be_Cleared()
 {
     Cart cart = new Cart();
     cart.AddItem(new Product(), 1);
     Assert.AreEqual(1, cart.Lines.Count);
     cart.Clear();
     Assert.AreEqual(0, cart.Lines.Count);
 }
Example #6
0
        public void Can_Add_Items_To_Cart()
        {
            Product p1 = new Product { ProductID = 1 };
            Product p2 = new Product { ProductID = 2 };

            Cart cart = new Cart();
            cart.AddItem(p1, 1);
            cart.AddItem(p1, 2);
            cart.AddItem(p2, 10);

            Assert.AreEqual(2, cart.Lines.Count, "Wrong number of lines in cart");

            var p1Line = cart.Lines.Where(l => l.Product.ProductID == 1).First();
            var p2Line = cart.Lines.Where(l => l.Product.ProductID == 2).First();
            Assert.AreEqual(3, p1Line.Quantity);
            Assert.AreEqual(10, p2Line.Quantity);
        }
Example #7
0
 public void Submitting_Empty_Shipping_Details_Displays_Default_View_With_Error()
 {
     // Arrange
     CartController controller = new CartController(null, null);
     Cart cart = new Cart();
     cart.AddItem(new Product(), 1);
     // Act
     var result = controller.CheckOut(cart, new FormCollection {
     { "Name", "" }
     });
     // Assert
     Assert.IsEmpty(result.ViewName);
     Assert.IsFalse(result.ViewData.ModelState.IsValid);
 }
Example #8
0
        public void Valid_Order_Goes_To_Submitter_And_Displays_Completed_View()
        {
            // Arrange
            var mockSubmitter = new Moq.Mock<IOrderSubmitter>();
            CartController controller = new CartController(null, mockSubmitter.Object);
            Cart cart = new Cart();
            cart.AddItem(new Product(), 1);
            var formData = new FormCollection {
            { "Name", "Steve" }, { "Line1", "123 My Street" },
            { "Line2", "MyArea" }, { "Line3", "" },
            { "City", "MyCity" }, { "State", "Some State" },
            { "Zip", "123ABCDEF" }, { "Country", "Far far away" },
            { "GiftWrap", bool.TrueString }
            };

            // Act
            var result = controller.CheckOut(cart, formData);

            // Assert
            Assert.AreEqual("Completed", result.ViewName);
            mockSubmitter.Verify(x => x.SubmitOrder(cart));
            Assert.AreEqual(0, cart.Lines.Count);
        }
        public void Summary_Action_Renders_View_With_Total()
        {
            CartController controller = new CartController(null, null);
            Cart cart = new Cart();
            Product p1 = new Product() { ProductID = 1, Price = 12 };
            Product p2 = new Product() { ProductID = 2, Price = 50 };
            cart.AddItem(p1, 12);
            cart.AddItem(p2, 5);

            ViewResult result = controller.Summary(cart);

            Assert.AreEqual(cart, ((Cart)result.ViewData.Model));
            Assert.AreEqual(cart.Lines.Count, ((Cart)result.ViewData.Model).Lines.Count);
            Assert.AreEqual(cart.ComputeTotalValue(), ((Cart)result.ViewData.Model).ComputeTotalValue());
        }