public void Cannot_Edit_Nonexistent_Product() { Mock<IEmployeeRepository> mock = new Mock<IEmployeeRepository>(); mock.Setup(m => m.Employees).Returns(new Employee[] { new Employee {EmployeeId = 1, Name = "Employee1"}, new Employee {EmployeeId = 2, Name = "Employee2"}, new Employee {EmployeeId = 3, Name = "Employee3"}, }.AsQueryable()); AdminController target = new AdminController(mock.Object); Employee result = (Employee)target.Edit(4).ViewData.Model; Assert.IsNull(result); }
public void Cannot_Save_Invalid_Changes() { Mock<IEmployeeRepository> mock = new Mock<IEmployeeRepository>(); AdminController target = new AdminController(mock.Object); Employee product = new Employee { Name = "Test" }; target.ModelState.AddModelError("error", "error"); ActionResult result = target.Edit(product, null); mock.Verify(m => m.SaveEmploee(It.IsAny<Employee>()), Times.Never()); Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void Can_Edit_Employee() { Mock<IEmployeeRepository> mock = new Mock<IEmployeeRepository>(); mock.Setup(m => m.Employees).Returns(new Employee[] { new Employee {EmployeeId = 1, Name = "Employee1"}, new Employee {EmployeeId = 2, Name = "Employee2"}, new Employee {EmployeeId = 3, Name = "Employee3"}, }.AsQueryable()); AdminController target = new AdminController(mock.Object); Employee p1 = target.Edit(1).ViewData.Model as Employee; Employee p2 = target.Edit(2).ViewData.Model as Employee; Employee p3 = target.Edit(3).ViewData.Model as Employee; Assert.AreEqual(1, p1.EmployeeId); Assert.AreEqual(2, p2.EmployeeId); Assert.AreEqual(3, p3.EmployeeId); }
public void Can_Save_Valid_Changes() { Mock<IEmployeeRepository> mock = new Mock<IEmployeeRepository>(); AdminController target = new AdminController(mock.Object); Employee product = new Employee { Name = "Test" }; ActionResult result = target.Edit(product, null); mock.Verify(m => m.SaveEmploee(product)); Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }