public void Can_Remove_Product_From_Cart()
        {
            // Подготовка
            var mockProductsRepository = new Moq.Mock<IProductsRepository>();
            var products = new List<Product>()
            {
                new Product {ProductID=14, Name="Much Ado About Nothing" },
                new Product {ProductID=27, Name="The Comedy of Errors" }
            };
            mockProductsRepository.Setup(x => x.Products)
                                  .Returns(products.AsQueryable());
            var cart = new Cart();
            var controller = new CartController(mockProductsRepository.Object, null);
            controller.AddToCart(cart, 14, "someReturnUrl");
            controller.AddToCart(cart, 27, "someReturnUrl");
            Assert.AreEqual(2, cart.Lines.Count);
            // Действие
            RedirectToRouteResult result =
                controller.RemoveFromCart(cart, 14, "someReturnUrl");
            // Утверждение
            Assert.AreEqual(1, cart.Lines.Count);
            Assert.AreEqual(27, cart.Lines[0].Product.ProductID);

            Assert.AreEqual("Index", result.RouteValues["action"]);
            Assert.AreEqual("someReturnUrl", result.RouteValues["returnUrl"]);
        }
示例#2
0
 public RedirectToRouteResult RemoveFromCart(Cart cart, int productID, string returnUrl)
 {
     Product product = productsRepository.Products
                         .FirstOrDefault(p => p.ProductID == productID);
     cart.RemoveLine(product);
     return RedirectToAction("Index", new { returnUrl });
 }
示例#3
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 });
 }
        public void SubmitOrder(Cart cart)
        {
            StringBuilder body = new StringBuilder();
            body.AppendLine("A new order has been submitted");
            body.AppendLine("---");
            body.AppendLine("Items:");
            foreach(var line in cart.Lines)
            {
                var subtotal = line.Product.Price * line.Quantity;
                body.AppendFormat("{0} x {1} (subtotal: {2:c})", line.Quantity,
                            line.Product.Name, subtotal);
            }
            body.AppendFormat("Total order value: {0:c}",
                cart.ComputeTotalValue());
            body.AppendLine("---");
            body.AppendLine("Ship to:");
            body.AppendLine(cart.ShippingDetails.Name);
            body.AppendLine(cart.ShippingDetails.Line1);
            body.AppendLine(cart.ShippingDetails.Line2 ?? "");
            body.AppendLine(cart.ShippingDetails.Line3 ?? "");
            body.AppendLine(cart.ShippingDetails.City);
            body.AppendLine(cart.ShippingDetails.State ?? "");
            body.AppendLine(cart.ShippingDetails.Country);
            body.AppendLine(cart.ShippingDetails.Zip);
            body.AppendLine("---");
            body.AppendFormat("Gift wrap: {0}",
                cart.ShippingDetails.GiftWrap ? "Yes" : "No");

            SmtpClient smtpClient = new SmtpClient(smtpServer);
            smtpClient.Send(new MailMessage(mailFrom, mailTo, MailSubject, body.ToString())); 
        }
示例#5
0
        public ViewResult Index(Cart cart, string returnUrl)
        {
            ViewData["Category"] = cart;
            ViewData["returnUrl"] = returnUrl;

            return View(cart);
        }
示例#6
0
 public void SubmitOrder(Cart cart)
 {
     StringBuilder body = new StringBuilder();
     body.AppendLine("Bạn đã đăng kí đơn đặt hàng này:");
     body.AppendLine("----------------------------------");
     body.AppendLine("Danh sách xe được mua:");
     foreach (var line in cart.Lines)
     {
         var subtotal = line.Product.Price * line.Quantity;
         body.AppendFormat("         {0} x {1} (Thành tiền: {2:c})" + Environment.NewLine, line.Quantity,line.Product.Name,subtotal);
     }
     body.AppendLine("----------------------------------");
     body.AppendFormat("Tổng tiền thanh toán: {0:c}" + Environment.NewLine, cart.ComputeTotalValue());
     body.AppendLine("--------***---------");
     body.AppendLine("Hàng hóa được chuyển đến:");
     body.AppendLine(cart.ShippingDetails.Name);
     body.AppendLine(cart.ShippingDetails.BirthOfDay.ToShortDateString());
     body.AppendLine(cart.ShippingDetails.Career ?? "");
     body.AppendLine(cart.ShippingDetails.Address ?? "");
     body.AppendLine(cart.ShippingDetails.City);
     body.AppendLine(cart.ShippingDetails.State ?? "");
     body.AppendLine(cart.ShippingDetails.Country);
     body.AppendLine(cart.ShippingDetails.Zip);
     body.AppendLine("---------***---------");
     body.AppendFormat("Nhận quà: {0}"+Environment.NewLine,cart.ShippingDetails.GiftWrap ? "Có" : "Không");
     body.AppendLine("---------***---------");
     body.AppendLine("Cám ơn bạn đã tin tưởng đặt mua hàng tại website chúng tôi!");
     body.AppendLine("Trân trọng kính chào quý khách.");
     SmtpClient smtpClient = new SmtpClient();
     smtpClient.Credentials = new NetworkCredential(mailFrom, pass);
     smtpClient.Port = int.Parse(serverport);
     smtpClient.Host = smtpServer;
     smtpClient.EnableSsl = true;
     smtpClient.Send(new MailMessage(mailFrom, cart.ShippingDetails.Email , MailSubject,body.ToString()));
 }
示例#7
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"]);
        }
示例#8
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());
 }
示例#9
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);
 }
示例#10
0
 public void Cart_Shipping_Details_Start_Empty()
 {
     Cart cart = new Cart();
     ShippingDetails d = cart.ShippingDetails;
     Assert.IsNull(d.Name);
     Assert.IsNull(d.Line1);
     Assert.IsNull(d.Line2);
     Assert.IsNull(d.Line3);
     Assert.IsNull(d.City);
     Assert.IsNull(d.Country);
     Assert.IsNull(d.Zip);
 }
 public void Index_Action_Renders_Default_View_With_Cart_And_ReturnUrl()
 {
     // Подготовка
     Cart cart = new Cart();
     CartController controller = new CartController(null,null);
     // Действие
     ViewResult result = controller.Index(cart, "myReturnUrl");
     // Утверждение
     Assert.IsEmpty(result.ViewName);
     Assert.AreSame(cart, result.ViewData.Model);
     Assert.AreEqual("myReturnUrl", result.ViewData["returnUrl"]);
     Assert.AreEqual("Cart", result.ViewData["CurrentCategory"]);
 }
示例#12
0
        public object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext)
        {
            if (bindingContext.Model != null)
                throw new InvalidOperationException("Cannot update instances");

            Cart cart = (Cart)controllerContext.HttpContext.Session[cartSessionKey];
            if (cart == null)
            {
                cart = new Cart();
                controllerContext.HttpContext.Session[cartSessionKey] = cart;
            }
            return cart;
        }
示例#13
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);
        }
        public object BindModel(ControllerContext controllerContext,
                                ModelBindingContext bindingContext)
        {
            if (bindingContext.Model != null)
                throw new InvalidOperationException("Не удалось обновить экземпляры");

            Cart cart = (Cart)controllerContext.HttpContext.Session[cartSessionKey];
            if (cart == null)
            {
                cart = new Cart();
                controllerContext.HttpContext.Session[cartSessionKey] = cart;
            }
            return cart; 
        }
示例#15
0
        public void Index_Action_Renders_Default_View_With_Cart_And_ReturnUrl()
        {
            // Set up the controller
            Cart cart = new Cart();
            CartController controller = new CartController(null, null);

            // Invoke action method
            ViewResult result = controller.Index(cart, "myReturnUrl");

            // Verify results
            Assert.IsEmpty(result.ViewName); // Renders default view
            Assert.AreSame(cart, result.ViewData.Model);
            Assert.AreEqual("myReturnUrl", result.ViewData["returnUrl"]);
            Assert.AreEqual("Cart", result.ViewData["CurrentCategory"]);
        }
 public ViewResult CheckOut(Cart cart, FormCollection form)
 {
     if(cart.Lines.Count == 0)
     {
         ModelState.AddModelError("Cart", "Sorry, your cart is empty!");
         return View();
     }
     if (TryUpdateModel(cart.ShippingDetails, form.ToValueProvider()))
     {
         orderSubmitter.SubmitOrder(cart);
         cart.Clear();
         return View("Completed");
     }
     else
         return View();
 }
示例#17
0
        public ViewResult CheckOut(Cart cart, FormCollection form)
        {
            // Empty carts can't be checked out
            if(cart.Lines.Count == 0) {
                ModelState.AddModelError("Cart", "Sorry, your cart is empty!");
                return View();
            }

            // Invoke model binding manually
            if (TryUpdateModel(cart.ShippingDetails, form.ToValueProvider())) {
                orderSubmitter.SubmitOrder(cart);
                cart.Clear();
                return View("Completed");
            }
            else // Something was invalid
                return View();
        }
示例#18
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);
        }
 public void Cad_Add_Product_To_Cart()
 {
     // Подгтовка
     var mockProductsRepository = new Moq.Mock<IProductsRepository>();
     var products = new List<Product>()
     {
         new Product {ProductID=14, Name="Much Ado About Nothing" },
         new Product {ProductID=27, Name="The Comedy of Errors" }
     };
     mockProductsRepository.Setup(x => x.Products)
                           .Returns(products.AsQueryable());
     var cart = new Cart();
     var controller = new CartController(mockProductsRepository.Object, null);
     // Действие
     RedirectToRouteResult result =
         controller.AddToCart(cart, 27, "someReturnUrl");
     // Утверждение
     Assert.AreEqual(1, cart.Lines.Count);
     Assert.AreEqual("The Comedy of Errors", cart.Lines[0].Product.Name);
     Assert.AreEqual(1, cart.Lines[0].Quantity);
     // Проверка перенаправления на экран отображения корзины
     Assert.AreEqual("Index", result.RouteValues["action"]);
     Assert.AreEqual("someReturnUrl", result.RouteValues["returnUrl"]);
 }
示例#20
0
 public void Cart_Starts_Empty()
 {
     Cart cart = new Cart();
     Assert.AreEqual(0, cart.Lines.Count);
     Assert.AreEqual(0, cart.ComputeTotalValue());
 }
示例#21
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);
        }
示例#22
0
 public void Submitting_Order_With_No_Lines_Displays_Default_View_With_Error()
 {
     // Arrange
     CartController controller = new CartController(null, null);
     Cart cart = new Cart();
     // Act
     var result = controller.CheckOut(cart, new FormCollection());
     // Assert
     Assert.IsEmpty(result.ViewName);
     Assert.IsFalse(result.ViewData.ModelState.IsValid);
 }
示例#23
0
 public void SubmitOrder(Cart cart)
 {
     // Do nothing
 }
 public void SubmitOrder(Cart cart)
 {
 }
 public ViewResult CheckOut(Cart cart)
 {
     return View(cart.ShippingDetails);
 }
 public ViewResult Summary(Cart cart)
 {
     return View(cart);
 }
示例#27
0
 public void Cart_Not_GiftWrapperd_By_Default() {
     Cart cart = new Cart();
     Assert.IsFalse(cart.ShippingDetails.GiftWrap);
 }
        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());
        }
示例#29
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);
 }
示例#30
0
 public ViewResult CheckOut(Cart cart)
 {
     return View(cart);
 }