public ActionResult Edit(Client client) { if (ModelState.IsValid) { repository.SaveClient(client); TempData["message"] = string.Format("{0} has been saved", client.Login); return RedirectToAction("List"); } else { // there is something wrong with the data values return View(client); } }
public void Cannot_Save_Invalid_Changes() { // Arrange - create mock repository Mock<IClientRepository> mock = new Mock<IClientRepository>(); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Arrange - create a client Client client = new Client { ContractNumber = "Test" }; // Arrange - add an error to the model state target.ModelState.AddModelError("error", "error"); // Act - try to save the client ActionResult result = target.Edit(client); // Assert - check that the repository was not called mock.Verify(m => m.SaveClient(It.IsAny<Client>()), Times.Never()); // Assert - check the method result type Assert.IsInstanceOfType(typeof(ViewResult), result); }
public void Can_Delete_Valid_Clients() { // Arrange - create a client Client cl = new Client { ClientId = 2, ContractNumber = "Test" }; // Arrange - create the mock repository Mock<IClientRepository> mock = new Mock<IClientRepository>(); mock.Setup(m => m.Clients).Returns(new Client[] { new Client {ClientId = 1, ContractNumber = "L1"}, cl, new Client {ClientId = 3, ContractNumber = "L3"}, }.AsQueryable()); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Act - delete the client target.Delete(cl.ClientId); // Assert - ensure that the repository delete method was // called with the correct client mock.Verify(m => m.DeleteClient(cl.ClientId)); }
public void SaveClient(Client client) { if (client.ClientId == 0) { context.Clients.Add(client); } else { Client dbEntry = context.Clients.Find(client.ClientId); if (dbEntry != null) { dbEntry.Login = client.Login; dbEntry.Address = client.Address; dbEntry.Email = client.Email; dbEntry.Password = client.Password; // dbEntry.Vip = client.Vip; } } context.SaveChanges(); }
public void Can_Save_Valid_Changes() { // Arrange - create mock repository Mock<IClientRepository> mock = new Mock<IClientRepository>(); // Arrange - create the controller AdminController target = new AdminController(mock.Object); // Arrange - create a client Client client = new Client { ContractNumber = "Test" }; // Act - try to save the client ActionResult result = target.Edit(client); // Assert - check that the repository was called mock.Verify(m => m.SaveClient(client)); // Assert - check the method result type Assert.IsNotInstanceOfType( typeof(ViewResult), result); }