public void Can_Edit_Product() { // Arrange - create the mock repository Mock <IProductRepository> mock = new Mock <IProductRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product { ProductID = 1, Name = "P1" }, new Product { ProductID = 2, Name = "P2" }, new Product { ProductID = 3, Name = "P3" }, }.AsQueryable()); // Arrange - create the controller ControlsystemController target = new ControlsystemController(mock.Object); // Act Product p1 = target.Edit(1).ViewData.Model as Product; Product p2 = target.Edit(2).ViewData.Model as Product; Product p3 = target.Edit(3).ViewData.Model as Product; // Assert Assert.AreEqual(1, p1.ProductID); Assert.AreEqual(2, p2.ProductID); Assert.AreEqual(3, p3.ProductID); }
public void Cannot_Checkout_Invalid_ShippingDetails() { // Arrange - create a mock order processor Mock <IOrderProcessor> mock = new Mock <IOrderProcessor>(); Mock <IProductRepository> mock1 = new Mock <IProductRepository>(); // Arrange - create a cart with an item Cart cart = new Cart(); cart.AddItem(new Product(), 1); Order order = new Order(); Shipping shipping = new Shipping(); // Arrange - create an instance of the controller CartController target = new CartController(null, mock.Object); ControlsystemController adm = new ControlsystemController(mock1.Object); // Arrange - add an error to the model target.ModelState.AddModelError("error", "error"); // Act - try to checkout ViewResult result = target.Checkout(order, new Shipping(), cart); ViewResult adresul = adm.Shippord(); // Assert - check that the order hasn't been passed on to the processor mock.Verify(m => m.ProcessOrder(It.IsAny <Cart>(), It.IsAny <Shipping>()), Times.Never()); // Assert - check that the method is returning the default view Assert.AreEqual("", adresul.ViewName); Assert.IsNotNull(adresul); // Assert.AreEqual(false, adresul.ViewData.ModelState.IsValid); // Assert - check that we are passing an invalid model to the view Assert.AreEqual(false, result.ViewData.ModelState.IsValid); }
public void Can_Delete_Valid_Products() { // Arrange - create a Product Product prod = new Product { ProductID = 2, Name = "Test" }; // Arrange - create the mock repository Mock <IProductRepository> mock = new Mock <IProductRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product { ProductID = 1, Name = "P1" }, prod, new Product { ProductID = 3, Name = "P3" }, }.AsQueryable()); // Arrange - create the controller ControlsystemController target = new ControlsystemController(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)); }
public void Index_Contains_All_Products() { // Arrange - create the mock repository Mock <IProductRepository> mock = new Mock <IProductRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product { ProductID = 1, Name = "P1" }, new Product { ProductID = 2, Name = "P2" }, new Product { ProductID = 3, Name = "P3" }, }.AsQueryable()); // Arrange - create a controller ControlsystemController target = new ControlsystemController(mock.Object); // Action Product[] result = ((IEnumerable <Product>)target.Mainpage().ViewData.Model).ToArray(); // Assert Assert.AreEqual(result.Length, 3); Assert.AreEqual("P1", result[0].Name); Assert.AreEqual("P2", result[1].Name); Assert.AreEqual("P3", result[2].Name); }
public void Can_Save_Valid_Changes() { // Arrange - create mock repository Mock <IProductRepository> mock = new Mock <IProductRepository>(); // Arrange - create the controller ControlsystemController target = new ControlsystemController(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 that 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 <IProductRepository> mock = new Mock <IProductRepository>(); // Arrange - create the controller ControlsystemController target = new ControlsystemController(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, null); // Assert - check that the repository was not called mock.Verify(m => m.SaveProduct(It.IsAny <Product>()), Times.Never()); // Assert - check the method result type Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void Cannot_Edit_Nonexistent_Product() { // Arrange - create the mock repository Mock <IProductRepository> mock = new Mock <IProductRepository>(); mock.Setup(m => m.Products).Returns(new Product[] { new Product { ProductID = 1, Name = "P1" }, new Product { ProductID = 2, Name = "P2" }, new Product { ProductID = 3, Name = "P3" }, }.AsQueryable()); // Arrange - create the controller ControlsystemController target = new ControlsystemController(mock.Object); // Act Product result = (Product)target.Edit(4).ViewData.Model; // Assert Assert.IsNull(result); }