public void Delete_should_remove_the_product()
        {
            // Arrange
            Product product = new Product
            {
                 ProductID = 1,
                 ProductName = "Product 1"
            };
            mock.Setup(m => m.Products.GetById(It.IsAny<int>())).Returns(product);
            mock.Setup(m => m.Products.Delete(It.IsAny<int>()));
            mock.Setup(m => m.SaveChanges());
            ProductController controller = new ProductController(mock.Object);

            // Act
            var result = controller.Delete(1) as RedirectToRouteResult;;
            var viewModel = controller.ViewData.Model as ProductsViewModel;

            // Assert
            // Check that each method was only called once.
            mock.Verify(x => x.Products.Delete(It.IsAny<int>()), Times.Once());
            mock.Verify(x => x.SaveChanges(), Times.Once());

            //check that redirec to index happened
            Assert.AreEqual("Index", result.RouteValues["action"]);
        }
        public ActionResult Create(ProductsViewModel productViewModel)
        {
            //get selected category
            var selectedCategory = this._unit.Categories.GetById(productViewModel.SelectedCategoryValue);
            //get selected supplier
            var selectedSupplier = this._unit.Suppliers.GetById(productViewModel.SelectedSupplierValue);

            //populate suppliers and categories in viewmodel in case validation fails
            productViewModel.Suppliers = this._unit.Suppliers.GetAll().AsEnumerable<Supplier>();
            productViewModel.Categories = this._unit.Categories.GetAll().AsEnumerable<Category>();

            //create a new product object that contains what the user entered
            Product newProduct = new Product
            {
                ProductName = productViewModel.Name,
                Supplier = selectedSupplier,
                Category = selectedCategory,
                UnitPrice = productViewModel.UnitPrice,
                UnitsInStock = productViewModel.UnitsInStock,
                UnitsOnOrder = productViewModel.UnitsOnOrder
            };

            try
            {
                if (ModelState.IsValid)
                {
                    this._unit.Products.Add(newProduct);
                    this._unit.SaveChanges();
                    TempData["CreateSuccess"] = true;
                    return RedirectToAction("Index");
                }
            }
            catch (DataException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ViewBag.DeleteError = true;
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
            return View(productViewModel);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ProductUpdatedEvent" /> class
 /// </summary>
 /// <param name="product">The <see cref="Product" /> which the event relates to</param>
 public ProductUpdatedEvent(Product product)
     : base(string.Format("Product: '{0}' was updated.", product.Name), "Northwind.Web", WebEventCodes.WebExtendedBase + 100)
 {
 }
        public void POST_Edit_should_save_updated_record()
        {
            // Arrange
            Supplier selectedSupplier = new Supplier
            {
                SupplierID = 99,
                CompanyName = "Supplier 99"
            };
            Category selectedCategory = new Category
            {
                CategoryID = 99,
                CategoryName = "Category 99"
            };

            Product dbproduct = new Product
            {
                ProductID = 1,
                ProductName = "Product 1",
                UnitPrice = 10.00M,
                UnitsInStock = 1,
                UnitsOnOrder = 1,
                CategoryID = 1,
                SupplierID = 1,
                Supplier = new Supplier
                {
                    SupplierID = 1,
                    CompanyName = "Supplier 1"
                },
                Category = new Category
                {
                    CategoryID = 1,
                    CategoryName = "Category 1"
                }
            };

            ProductsViewModel productViewModel = new ProductsViewModel
            {
                ID = 1,
                Name = "Product Romeo",
                UnitPrice = 20.00M,
                UnitsInStock = 3,
                UnitsOnOrder = 3,
                SelectedCategoryValue = 99,
                SelectedSupplierValue = 99
            };

            mock.Setup(m => m.Suppliers.GetById(It.IsAny<int>())).Returns(selectedSupplier);
            mock.Setup(m => m.Categories.GetById(It.IsAny<int>())).Returns(selectedCategory);
            mock.Setup(m => m.Products.GetById(It.IsAny<int>())).Returns(dbproduct);
            mock.Setup(m => m.Suppliers.GetAll()).Returns(suppliers.AsQueryable<Supplier>());
            mock.Setup(m => m.Categories.GetAll()).Returns(categories.AsQueryable<Category>());
            mock.Setup(m => m.Products.Update(It.IsAny<Product>()));
            mock.Setup(m => m.SaveChanges());

            ProductController controller = new ProductController(mock.Object);

            //act
            var result = controller.Edit(productViewModel) as ViewResult;
            var viewModel = controller.ViewData.Model as ProductsViewModel;

            // Assert
            // Check that each method was only called once.
            mock.Verify(x => x.Products.Update(It.IsAny<Product>()), Times.Once());
            mock.Verify(x => x.SaveChanges(), Times.Once());
            Assert.AreEqual(dbproduct.ProductID, viewModel.ID);
            Assert.AreEqual(dbproduct.ProductName, viewModel.Name);
            Assert.AreEqual(dbproduct.UnitPrice, viewModel.UnitPrice);
            Assert.AreEqual(dbproduct.UnitsInStock, viewModel.UnitsInStock);
            Assert.AreEqual(dbproduct.UnitsOnOrder, viewModel.UnitsOnOrder);
            Assert.AreEqual(dbproduct.Supplier, selectedSupplier);
            Assert.AreEqual(dbproduct.Category, selectedCategory);
            Assert.AreEqual("", result.ViewName);
        }
        public void Edit_should_allow_edit_for_a_specific_product()
        {
            // Arrange
            Product product = new Product
            {
                ProductID = 1,
                ProductName = "Product 1",
                UnitPrice = 10.00M,
                UnitsInStock = 1,
                UnitsOnOrder = 1,
                CategoryID = 1,
                SupplierID = 1
            };
            mock.Setup(m => m.Products.GetById(It.IsAny<int>())).Returns(product);
            Mock<IProductsViewModel> mockProductViewModel = new Mock<IProductsViewModel>();
            mock.Setup(m => m.Suppliers.GetAll()).Returns(suppliers.AsQueryable<Supplier>());
            mock.Setup(m => m.Categories.GetAll()).Returns(categories.AsQueryable<Category>());
            ProductController controller = new ProductController(mock.Object);

            //act
            ViewResult result = controller.Edit(1) as ViewResult;
            var viewModel = controller.ViewData.Model as ProductsViewModel;

            // Assert
            Assert.AreEqual(product.ProductID,viewModel.ID);
            Assert.AreEqual(product.ProductName, viewModel.Name);
            Assert.AreEqual(product.UnitPrice, viewModel.UnitPrice);
            Assert.AreEqual(product.UnitsInStock, viewModel.UnitsInStock);
            Assert.AreEqual(product.UnitsOnOrder, viewModel.UnitsOnOrder);
            Assert.AreEqual(product.CategoryID, viewModel.SelectedCategoryValue);
            Assert.AreEqual(product.SupplierID, viewModel.SelectedSupplierValue);
            Assert.AreEqual("", result.ViewName);
        }