示例#1
0
 public ViewResult Index(Cart cart, string returnUrl)
 {
     return View(new CartIndexViewModel
     {
         Cart = cart,
         ReturnUrl = returnUrl
     });
 }
示例#2
0
        public RedirectToRouteResult RemoveFromCart(Cart cart, int productId, string returnUrl)
        {
            Product product = repository.Products
                .FirstOrDefault(p => p.ProductId == productId);

            if (product != null)
            {
                cart.RemoveLine(product);
            }
            return RedirectToAction("Index", new { returnUrl });
        }
示例#3
0
        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 });
        }
示例#4
0
 public void Can_Add_New_Lines() {
     // Arrange - create some test products
     Product p1 = new Product { ProductId = 1, ProductName = "P1" };
     Product p2 = new Product { ProductId = 2, ProductName = "P2" };
     // Arrange - create a new cart
     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);
 }
示例#5
0
 public void Calculate_Cart_Total()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductId = 1, ProductName = "P1", Price = 100M };
     Product p2 = new Product { ProductId = 2, ProductName = "P2", Price = 50M };
     // Arrange - create a new cart
     Cart target = new Cart();
     // Act
     target.AddItem(p1, 1);
     target.AddItem(p2, 1);
     target.AddItem(p1, 3);
     decimal result = target.ComputeTotalValue();
     // Assert
     Assert.AreEqual(result, 450M);
 }
示例#6
0
 public void Can_Add_Quantity_For_Existing_Lines() {
     // Arrange - create some test products
     Product p1 = new Product { ProductId = 1, ProductName = "P1" };
     Product p2 = new Product { ProductId = 2, ProductName = "P2" };
     // Arrange - create a new cart
     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);
 }
示例#7
0
 public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
 {
     if (cart.Lines.Count() == 0)
     {
         ModelState.AddModelError("", "Sorry, your cart is empty!");
     }
     if (ModelState.IsValid)
     {
         orderProcessor.ProcessOrder(cart, shippingDetails);
         cart.Clear();
         return View("Completed");
     }
     else
     {
         return View(shippingDetails);
     }
 }
示例#8
0
 public void Can_Remove_Line()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductId = 1, ProductName = "P1" };
     Product p2 = new Product { ProductId = 2, ProductName = "P2" };
     Product p3 = new Product { ProductId = 3, ProductName = "P3" };
     // Arrange - create a new cart
     Cart target = new Cart();
     // Arrange - add some products to the cart
     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);
 }
示例#9
0
 public object BindModel(ControllerContext controllerContext,
     ModelBindingContext bindingContext)
 {
     // get the Cart from the session
     Cart cart = null;
     if (controllerContext.HttpContext.Session != null)
     {
         cart = (Cart)controllerContext.HttpContext.Session[sessionKey];
     }
     // create the Cart if there wasn't one in the session data
     if (cart == null)
     {
         cart = new Cart();
         if (controllerContext.HttpContext.Session != null)
         {
             controllerContext.HttpContext.Session[sessionKey] = cart;
         }
     }
     // return the cart
     return cart;
 }
示例#10
0
        public void Can_View_Cart_Contents()
        {
            // Arrange - create a Cart
            Cart cart = new Cart();

            // Arrange - create the controller
            CartController target = new CartController(null);

            // Act - call the Index action method
            CartIndexViewModel result
                = (CartIndexViewModel)target.Index(cart, "myUrl").ViewData.Model;

            // Assert
            Assert.AreSame(result.Cart, cart);
            Assert.AreEqual(result.ReturnUrl, "myUrl");
        }
示例#11
0
 public PartialViewResult Summary(Cart cart)
 {
     return PartialView(cart);
 }
示例#12
0
 public void Can_Clear_Contents()
 {
     // Arrange - create some test products
     Product p1 = new Product { ProductId = 1, ProductName = "P1", Price = 100M };
     Product p2 = new Product { ProductId = 2, ProductName = "P2", Price = 50M };
     // Arrange - create a new cart
     Cart target = new Cart();
     // Arrange - add some items
     target.AddItem(p1, 1);
     target.AddItem(p2, 1);
     // Act - reset the cart
     target.Clear();
     // Assert
     Assert.AreEqual(target.Lines.Count(), 0);
 }
示例#13
0
        public void Can_Add_To_Cart()
        {
            // Arrange - create the mock repository
            Mock<IShoeStoreRepository> mock = new Mock<IShoeStoreRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] {
                new Product {ProductId = 1, ProductName = "P1", ProductCategoryId = 1},
            }.AsQueryable());

            // Arrange - create a Cart
            Cart cart = new Cart();

            // Arrange - create the controller
            CartController target = new CartController(mock.Object);

            // Act - add a product to the cart
            target.AddToCart(cart, 1, null);

            // Assert
            Assert.AreEqual(cart.Lines.Count(), 1);
            Assert.AreEqual(cart.Lines.ToArray()[0].Product.ProductId, 1);
        }
示例#14
0
        public void Adding_Product_To_Cart_Goes_To_Cart_Screen()
        {
            // Arrange - create the mock repository
            Mock<IShoeStoreRepository> mock = new Mock<IShoeStoreRepository>();
            mock.Setup(m => m.Products).Returns(new Product[] {
                new Product {ProductId = 1, ProductName = "P1", ProductCategoryId = 1},
            }.AsQueryable());

            // Arrange - create a Cart
            Cart cart = new Cart();

            // Arrange - create the controller
            CartController target = new CartController(mock.Object);

            // Act - add a product to the cart
            RedirectToRouteResult result = target.AddToCart(cart, 2, "myUrl");

            // Assert
            Assert.AreEqual(result.RouteValues["action"], "Index");
            Assert.AreEqual(result.RouteValues["returnUrl"], "myUrl");
        }
示例#15
0
        public void Can_Checkout_And_Submit_Order()
        {
            // Arrange - create a mock order processor
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
            // Arrange - create a cart with an item
            Cart cart = new Cart();
            cart.AddItem(new Product(), 1);
            // Arrange - create an instance of the controller
            CartController target = new CartController(null, mock.Object);

            // Act - try to checkout
            ViewResult result = target.Checkout(cart, new ShippingDetails());

            // Assert - check that the order has been passed on to the processor
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()),
                Times.Once());
            // Assert - check that the method is returning the Completed view
            Assert.AreEqual("Completed", result.ViewName);
            // Assert - check that I am passing a valid model to the view
            Assert.AreEqual(true, result.ViewData.ModelState.IsValid);
        }
示例#16
0
        public void Cannot_Checkout_Invalid_ShippingDetails()
        {

            // Arrange - create a mock order processor
            Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
            // Arrange - create a cart with an item
            Cart cart = new Cart();
            cart.AddItem(new Product(), 1);

            // Arrange - create an instance of the controller
            CartController target = new CartController(null, mock.Object);
            // Arrange - add an error to the model
            target.ModelState.AddModelError("error", "error");

            // Act - try to checkout
            ViewResult result = target.Checkout(cart, new ShippingDetails());

            // Assert - check that the order hasn't been passed on to the processor
            mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()),
                Times.Never());
            // Assert - check that the method is returning the default view
            Assert.AreEqual("", result.ViewName);
            // Assert - check that I am passing an invalid model to the view
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }
示例#17
0
 public void Cannot_Checkout_Empty_Cart()
 {
     // Arrange - create a mock order processor
     Mock<IOrderProcessor> mock = new Mock<IOrderProcessor>();
     // Arrange - create an empty cart
     Cart cart = new Cart();
     // Arrange - create shipping details
     ShippingDetails shippingDetails = new ShippingDetails();
     // Arrange - create an instance of the controller
     CartController target = new CartController(null, mock.Object);
     // Act
     ViewResult result = target.Checkout(cart, shippingDetails);
     // Assert - check that the order hasn't been passed on to the processor
     mock.Verify(m => m.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()),
     Times.Never());
     // Assert - check that the method is returning the default view
     Assert.AreEqual("", result.ViewName);
     // Assert - check that I am passing an invalid model to the view
     Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
 }