public ActionResult Edit(Product product) { if (ModelState.IsValid) { repository.SaveProduct(product); TempData["message"] = string.Format("{0} has been saved", product.Name); return RedirectToAction("Index"); } else { //There is something wrong with the data values return View(product); } }
//Adds a item or quanity to the shopping cart for a specific product public void AddItem(Product product, int quantity) { CartLine line = lineCollection .Where(p => p.Product.ProductID == product.ProductID) .FirstOrDefault(); if (line == null) { lineCollection.Add(new CartLine { Product = product, Quantity = quantity }); } else { line.Quantity += quantity; } }
//Saves a new Product to the database. public void SaveProduct(Product product) { if (product.ProductID == 0) { context.Products.Add(product); } else { Product dbEntry = context.Products.Find(product.ProductID); if (dbEntry != null) { dbEntry.Name = product.Name; dbEntry.Description = product.Description; dbEntry.Price = product.Price; dbEntry.Category = product.Category; } } context.SaveChanges(); }
public void Cann_Save_Valid_Changes() { //Arrange - Create mock repository Mock<IProductsRepository> mock = new Mock<IProductsRepository>(); //Arrange - Create the controller AdminController target = new AdminController(mock.Object); //Arrange - Create a Product Product product = new Product { Name = "Test" }; //Act - Try to save the product ActionResult result = target.Edit(product); //Assert - Check if the repository was called mock.Verify(m => m.SaveProduct(product)); //Assert - Check the method result type Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }
public void Cannot_Save_Invalid_Changes() { //Arrange - Create mock repository Mock<IProductsRepository> mock = new Mock<IProductsRepository>(); //Arrange - Create the controller AdminController target = new AdminController(mock.Object); //Arrange - Create a Product Product product = new Product { Name = "Test" }; //Arrange - Add an error to the model state target.ModelState.AddModelError("error", "error"); //Act - try to save the product ActionResult result = target.Edit(product); //Assert - Check that the repository was not called mock.Verify(m => m.SaveProduct(It.IsAny<Product>()), Times.Never()); //Assert - Check the method result Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void Can_Delete_Valid_Products() { //Arrange - Create a Product Product prod = new Product { ProductID = 2, Name = "Test" }; //Arrange - Create the mock repository Mock<IProductsRepository> mock = new Mock<IProductsRepository>(); mock.Setup(m => m.Products).Returns(new Product[]{ new Product {ProductID = 1, Name = "P1"}, prod, new Product { ProductID = 3, Name = "P3"}, }); //Arrange - Create the controller AdminController target = new AdminController(mock.Object); //Act - Delete the product target.Delete(prod.ProductID); //Assert - Ensure that the repository delete method was called with the correct Product mock.Verify(m => m.DeleteProduct(prod.ProductID)); }
//Removes the entire line item for the specific product public void RemoveLine(Product product) { lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID); }