public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl) { var product = _repository.Products.FirstOrDefault(p => p.ProductID == productId); if (product != null) { cart.AddProduct(product, 1); } return RedirectToAction("Index", new { returnUrl }); }
public virtual RedirectToRouteResult AddToCart(Cart cart, int productID, string returnUrl) { var product = this.repository.GetProducts().FirstOrDefault(x => x.ProductID == productID); if (product != null) { cart.AddProduct(product, 1); } return RedirectToAction(MVC.Cart.Index(cart, returnUrl)); }
public void CanClearContents() { //array var prod1 = new Product { ProductID = 1, Name = "p1", Price = 100M }; var prod2 = new Product { ProductID = 2, Name = "p2", Price = 50M }; //arrange var target = new Cart(); target.AddProduct(prod1, 2); target.AddProduct(prod1, 8); target.AddProduct(prod2, 1); //act target.Clear(); //assert Assert.AreEqual(0, target.Lines.Count()); }
public void CalculateCartTotal() { //arrange var prod1 = new Product { ProductID = 1, Name = "p1", Price = 100M}; var prod2 = new Product { ProductID = 2, Name = "p2", Price = 50M}; //arrange var target = new Cart(); //act target.AddProduct(prod1, 2); target.AddProduct(prod1, 8); target.AddProduct(prod2, 1); var result = target.CoputeTotalValue(); //assert Assert.AreEqual(1050M, result); }
public void CanRemoveLine() { //arrange var prod1 = new Product { ProductID = 1, Name = "p1" }; var prod2 = new Product { ProductID = 2, Name = "p2" }; var prod3 = new Product { ProductID = 3, Name = "p3" }; //array var target = new Cart(); //arrange target.AddProduct(prod1, 1); target.AddProduct(prod2, 3); target.AddProduct(prod3, 5); target.AddProduct(prod2, 1); //act target.RemoveLine(prod2); //assert Assert.AreEqual(0, target.Lines.Count(l => l.Product.ProductID == prod2.ProductID)); Assert.AreEqual(2, target.Lines.Count()); }
public void CanAddQuantityForExistingLines() { //arrange var prod1 = new Product {ProductID = 1, Name = "p1"}; var prod2 = new Product { ProductID = 2, Name = "p2" }; var prod3 = new Product { ProductID = 3, Name = "p3" }; //arrange var target = new Cart(); //act target.AddProduct(prod1, 10); target.AddProduct(prod1, 1); target.AddProduct(prod2, 1); target.AddProduct(prod3, 2); var results = target.Lines.OrderBy(l => l.Product.ProductID); //assert var linesArray = results.ToArray(); Assert.AreEqual(linesArray.Length, 3); Assert.AreEqual(linesArray[0].Product.ProductID, 1); Assert.AreEqual(linesArray[0].Quantity, 11); Assert.AreEqual(linesArray[1].Quantity, 1); Assert.AreEqual(linesArray[2].Quantity, 2); }
public void CanAddNewLies() { //arrange var prod1 = new Product { ProductID = 1, Name = "p1" }; var prod2 = new Product { ProductID = 2, Name = "p2" }; //arrange var target = new Cart(); //act target.AddProduct(prod1, 1); target.AddProduct(prod2, 2); var results = target.Lines.OrderBy(l => l.Product.ProductID); //assert var linesArray = results.ToArray(); Assert.AreEqual(2, linesArray.Length); Assert.AreEqual(1, linesArray[0].Product.ProductID); Assert.AreEqual(2, linesArray[1].Product.ProductID); Assert.AreEqual(1, linesArray[0].Quantity); Assert.AreEqual(2, linesArray[1].Quantity); }
public void whene_there_are_no_errors_it_should_redirect_to_a_completed_action() { var sut = CartControllerTests.GetCartController(); var cart = new Cart(); var shippingDetails = new ShippingDetails(); cart.AddProduct(Builder<Product>.CreateNew().Build(), 3); var res = sut.Checkout("~/", cart, shippingDetails) as RedirectToRouteResult; res.RouteValues["action"].ToString().Should().Be("OrderCompleted"); res.RouteValues["returnUrl"].ToString().Should().Be("~/"); sut.ModelState.IsValid.Should().BeTrue(); CartControllerTests.OrderProcessorMock.Verify(x => x.ProcessOrder(It.IsAny<Cart>(), It.IsAny<ShippingDetails>()), Times.Once()); }
public void it_should_render_the_default_view_passing_the_returnUrl_parameter() { var sut = CartControllerTests.GetCartController(); var cart = new Cart(); cart.AddProduct(new Product(), 2); var res = sut.Checkout("~/", cart); res.ViewName.Should().BeEmpty(); // default view rendered res.ViewData["ReturnUrl"].ToString().Should().Be("~/"); }