public void POST_Create_should_save_the_record_and_redirect_to_index()
        {
            // Arrange
            Supplier selectedSupplier = new Supplier
            {
                 SupplierID = 99,
                 CompanyName = "Supplier 99"
            };
            Category selectedCategory = new Category
            {
                 CategoryID = 99,
                 CategoryName = "Category 99"
            };
            ProductsViewModel productViewModel = new ProductsViewModel
            {
                 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.Suppliers.GetAll()).Returns(suppliers.AsQueryable<Supplier>());
            mock.Setup(m => m.Categories.GetAll()).Returns(categories.AsQueryable<Category>());

            mock.Setup(m => m.Products.Add(It.IsAny<Product>()));
            mock.Setup(m => m.SaveChanges());

            ProductController controller = new ProductController(mock.Object);

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

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

            //check that redirec to index happened
            Assert.AreEqual("Index", result.RouteValues["action"]);
        }
        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);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="SupplierUpdatedEvent" /> class
 /// </summary>
 /// <param name="supplier">The <see cref="Supplier" /> which the event relates to</param>
 public SupplierUpdatedEvent(Supplier supplier)
     : base(string.Format("Supplier: '{0}' was updated.", supplier.Name), "Northwind.Web", WebEventCodes.WebExtendedBase + 100)
 {
 }