public void SetNewValuesToLake_AndSetSuccessMessage_InTempData_IfEditNotFailed() { // Arrange var mockedLakeFactory = new Mock <ILakeFactory>(); var mockedLocationFactory = new Mock <ILocationFactory>(); var mockedLake = new Lake() { Name = "Test lake", Info = "Test info" }; var mockedLakeService = new Mock <ILakeService>(); mockedLakeService.Setup(s => s.FindByName(It.IsAny <string>())).Returns(mockedLake).Verifiable(); mockedLakeService.Setup(s => s.Save()).Verifiable(); var mockedLocationService = new Mock <ILocationService>(); var mockedFishService = new Mock <IFishService>(); var controller = new LakeController(mockedLakeFactory.Object, mockedLocationFactory.Object, mockedLakeService.Object, mockedLocationService.Object, mockedFishService.Object); var model = new EditLakeViewModel() { LakeName = "Test name", OldName = "Test name", LakeInfo = "Test info" }; // Act var result = controller.Edit(model, model.OldName) as ViewResult; // Assert Assert.AreEqual(model.LakeName, mockedLake.Name); Assert.AreEqual(model.LakeInfo, mockedLake.Info); Assert.AreEqual(GlobalMessages.EditLakeSuccessMessage, result.TempData[GlobalMessages.SuccessEditKey]); mockedLakeService.Verify(s => s.FindByName(It.IsAny <string>()), Times.Once); mockedLakeService.Verify(s => s.Save(), Times.Once); }
public void SetErrorMessage_InTempData_IfEditFailed() { // Arrange var mockedLakeFactory = new Mock <ILakeFactory>(); var mockedLocationFactory = new Mock <ILocationFactory>(); var mockedLake = new Lake() { Name = "Test lake", Info = "Test info" }; var mockedLakeService = new Mock <ILakeService>(); mockedLakeService.Setup(s => s.FindByName(It.IsAny <string>())).Returns(mockedLake).Verifiable(); mockedLakeService.Setup(s => s.Save()).Throws <Exception>(); var mockedLocationService = new Mock <ILocationService>(); var mockedFishService = new Mock <IFishService>(); var controller = new LakeController(mockedLakeFactory.Object, mockedLocationFactory.Object, mockedLakeService.Object, mockedLocationService.Object, mockedFishService.Object); var model = new EditLakeViewModel() { LakeName = "Test name", OldName = "Test name", LakeInfo = "Test info" }; // Act var result = controller.Edit(model, model.OldName) as ViewResult; // Assert Assert.AreEqual(GlobalMessages.EditLakeFailMessage, result.TempData[GlobalMessages.FailKey]); }
public ActionResult Edit(string name) { var lake = this.lakeService.FindByName(name); var model = new EditLakeViewModel(); model.OldName = lake.Name; model.LakeName = lake.Name; model.LakeInfo = lake.Info; return(View(model)); }
public ActionResult Edit(EditLakeViewModel model, string oldName) { if (ModelState.IsValid) { var lake = this.lakeService.FindByName(oldName); try { lake.Name = model.LakeName; lake.Info = model.LakeInfo; this.lakeService.Save(); TempData[GlobalMessages.SuccessEditKey] = GlobalMessages.EditLakeSuccessMessage; } catch (Exception) { TempData[GlobalMessages.FailKey] = GlobalMessages.EditLakeFailMessage; } } return(View(model)); }