public void Can_Update_Cart() { // Arrange // - create a mock repository Product p1 = new Product { ProductId = 1, Name = "P1" }; Product p2 = new Product { ProductId = 2, Name = "P2" }; Mock <IStoreRepository> mockRepo = new Mock <IStoreRepository>(); mockRepo.Setup(m => m.Products).Returns((new Product[] { p1, p2 }).AsQueryable); // - create a cart CartModel cartModel = new CartModel(); cartModel.AddItem(p1, 2); cartModel.AddItem(p2, 1); // Action Cart cart = new Cart(mockRepo.Object, cartModel); cart.OnPost(1, "myUrl"); cart.OnPost(2, "myUrl"); // Assert Assert.Equal(2, cartModel.Lines.Count()); Assert.Equal("P1", cartModel.Lines.First().Product.Name); Assert.Equal(3, cartModel.Lines.First().Quantity); }
public static bool AddToCart(string productName, string productPrice, string ProdectGroup, string PartID, string queryString, string productImage, string querytrimid) { var garageid = HttpContext.Current.Session["UserID"].ToString(); //var queryString = HttpContext.Current.Request.QueryString["ID"]; CartModel cart = new CartModel(Convert.ToInt32(queryString), Convert.ToInt32(garageid), PartID, 1, productName, Convert.ToDouble(productPrice), productImage, querytrimid); bool insertWork = cart.AddItem(); return((insertWork) ? true : false); }
public RedirectToActionResult AddToCart(int id, string returnUrl) { ProductModel product = _context.Products.Find(id); if (product != null) { cart.AddItem(product, 1); } return(RedirectToAction("Index", new { returnUrl })); }
public void Calculate_Cart_Total() { // Arrange Product p1 = new Product { ProductId = 1, Name = "P1", Price = 100m }; Product p2 = new Product { ProductId = 2, Name = "P2", Price = 50m }; CartModel cart = new CartModel(); cart.AddItem(p1, 1); cart.AddItem(p2, 1); cart.AddItem(p1, 10); // Act decimal total = cart.ComputeTotalValue(); // Assert Assert.Equal(1150, total); }
public void Can_Clear_Cart() { // Arrange Product p1 = new Product { ProductId = 1, Name = "P1", Price = 100m }; Product p2 = new Product { ProductId = 2, Name = "P2", Price = 50m }; CartModel cart = new CartModel(); cart.AddItem(p1, 1); cart.AddItem(p2, 1); cart.AddItem(p1, 10); // Act cart.Clear(); // Assert Assert.Empty(cart.Lines); }
public void Can_Remove_Line() { // Arrange Product p1 = new Product { ProductId = 1, Name = "P1" }; Product p2 = new Product { ProductId = 2, Name = "P2" }; CartModel cart = new CartModel(); cart.AddItem(p1, 1); cart.AddItem(p2, 1); cart.AddItem(p1, 10); // Act cart.RemoveLine(p2); // Assert Assert.Single(cart.Lines); Assert.Empty(cart.Lines.Where(line => line.Product.ProductId == p2.ProductId)); }
public void Can_Add_New_Lines() { // Arrange Product p1 = new Product { ProductId = 1, Name = "P1" }; Product p2 = new Product { ProductId = 2, Name = "P2" }; CartModel cart = new CartModel(); // Act cart.AddItem(p1, 1); cart.AddItem(p2, 1); CartLine[] results = cart.Lines.ToArray(); // Assert Assert.Equal(2, results.Length); Assert.Equal(p1, results[0].Product); Assert.Equal(p2, results[1].Product); }
public void Can_Checkout_And_Submit_Order() { // Arrange - create a mock order repository Mock <IOrderRepository> mock = new Mock <IOrderRepository>(); // - create a cart with one item CartModel cart = new CartModel(); cart.AddItem(new Product(), 1); // - create an instance of the controller OrderController target = new OrderController(mock.Object, cart); // Act - try to checkout RedirectToPageResult result = target.Checkout(new Order()) as RedirectToPageResult; // Assert - check that the order has been stored mock.Verify(m => m.SaveOrder(It.IsAny <Order>()), Times.Once); // - check that the method is redirecting to the Completed action Assert.Equal("/Completed", result.PageName); }
public void Cannot_Checkout_Invalid_ShippingDetails() { // Arrange - create a mock order repository Mock <IOrderRepository> mock = new Mock <IOrderRepository>(); // - create a cart with one item CartModel cart = new CartModel(); cart.AddItem(new Product(), 1); // - create an instance of the controller OrderController controller = new OrderController(mock.Object, cart); // - add an error to the model controller.ModelState.AddModelError("error", "error"); // Act - try to checkout ViewResult result = controller.Checkout(new Order()) as ViewResult; // Assert - check that the order hasn't been passed stored mock.Verify(m => m.SaveOrder(It.IsAny <Order>()), Times.Never); // - check that the method is returning the default view Assert.True(string.IsNullOrEmpty(result.ViewName)); // - check that I am passing an invalid model to the view Assert.False(result.ViewData.ModelState.IsValid); }