public void Can_Edit_Product() { var mock = new Mock<IProductRepository>(); mock.Setup(m => m.Products).Returns(new[] { new Product {ProductID = 1, Name = "p1"}, new Product {ProductID = 2, Name = "p2"}, new Product {ProductID = 3, Name = "p3"} }.AsQueryable()); var target = new AdminController(mock.Object); var p1 = target.Edit(1).ViewData.Model as Product; var p2 = target.Edit(2).ViewData.Model as Product; var p3 = target.Edit(3).ViewData.Model as Product; Assert.AreEqual(p1.ProductID, 1); Assert.AreEqual(p2.ProductID, 2); Assert.AreEqual(p3.ProductID, 3); }
public void Cannot_Save_Invalid_Changes() { Mock<IProductRepository> mock = new Mock<IProductRepository>(); var target = new AdminController(mock.Object); var product = new Product { Name = "Test" }; target.ModelState.AddModelError("error","error"); var result = target.Edit(product, null); mock.Verify(m => m.SaveProduct(It.IsAny<Product>()), Times.Never); Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void Cannot_Edit_Nonexistent_Product() { var mock = new Mock<IProductRepository>(); mock.Setup(m => m.Products).Returns(new[] { new Product {ProductID = 1, Name = "p1"}, new Product {ProductID = 2, Name = "p2"}, new Product {ProductID = 3, Name = "p3"} }.AsQueryable()); var target = new AdminController(mock.Object); var result = (Product) target.Edit(4).ViewData.Model; Assert.IsNull(result); }
public void Cannot_Edit_Nonexistent_Furn() { // Организация - создание имитированного хранилища данных Mock<IFurnitureRepository> mock = new Mock<IFurnitureRepository>(); mock.Setup(m => m.Furnitures).Returns(new List<Furniture> { new Furniture { Id = 1, Name = "Мебель1", Description = "Описание", Section = "Раздел", Price = 100}, new Furniture { Id = 2, Name = "Мебель2", Description = "Описание", Section = "Раздел", Price = 100}, new Furniture { Id = 3, Name = "Мебель3", Description = "Описание", Section = "Раздел", Price = 100}, new Furniture { Id = 4, Name = "Мебель4", Description = "Описание", Section = "Раздел", Price = 100}, new Furniture { Id = 5, Name = "Мебель5", Description = "Описание", Section = "Раздел", Price = 100}, }); // Организация - создание контроллера AdminController controller = new AdminController(mock.Object); // Действие Furniture result = controller.Edit(6).ViewData.Model as Furniture; }
public void Cannot_Save_Invalid_Changes() { // Организация - создание имитированного хранилища данных Mock<IFurnitureRepository> mock = new Mock<IFurnitureRepository>(); // Организация - создание контроллера AdminController controller = new AdminController(mock.Object); // Организация - создание объекта Game Furniture furniture = new Furniture { Id = 1, Name = "Мебель1", Description = "Описание", Section = "Раздел", Price = 100 }; // Организация - добавление ошибки в состояние модели controller.ModelState.AddModelError("error", "error"); // Действие - попытка сохранения товара ActionResult result = controller.Edit(furniture); // Утверждение - проверка того, что обращение к хранилищу НЕ производится mock.Verify(m => m.UpdateFurniture(It.IsAny<Furniture>()), Times.Never()); // Утверждение - проверка типа результата метода Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void Can_Save_Valid_Changes() { Mock<IProductRepository> mock = new Mock<IProductRepository>(); var target = new AdminController(mock.Object); var product = new Product {Name = "Test"}; var result = target.Edit(product, null); mock.Verify(m => m.SaveProduct(product)); Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }
public void Can_Save_Valid_Changes() { // Организация - создание имитированного хранилища данных Mock<IFurnitureRepository> mock = new Mock<IFurnitureRepository>(); // Организация - создание контроллера AdminController controller = new AdminController(mock.Object); // Организация - создание объекта Game Furniture furniture = new Furniture { Id = 1, Name = "Мебель1", Description = "Описание", Section = "Раздел", Price = 100 }; // Действие - попытка сохранения товара ActionResult result = controller.Edit(furniture); // Утверждение - проверка того, что к хранилищу производится обращение mock.Verify(m => m.UpdateFurniture(furniture)); // Утверждение - проверка типа результата метода Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }