public async Task GetUpdateShouldReturnViewWithValidModel() { // Arrange var drugGroupDb = DataHelper.GetDrugGroup(); var adminDrugGroupsService = new Mock <IAdminDrugGroupsService>(); adminDrugGroupsService .Setup(s => s.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(drugGroupDb); var controller = new DrugGroupsController(adminDrugGroupsService.Object, null); // Act var result = await controller.Update(drugGroupDb.Id); // Assert result.Should().NotBeNull(); result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().ViewName.Should().BeNull(); var model = result.As <ViewResult>().Model; model.Should().BeOfType <DrugGroupFormModel>(); var formModel = model.As <DrugGroupFormModel>(); formModel.Id.Should().Be(drugGroupDb.Id); formModel.Name.Should().Be(drugGroupDb.Name); formModel.Admin.Name.Should().Be(drugGroupDb.Admin.Name); formModel.AdminId.Should().Be(drugGroupDb.AdminId); }
public async Task GetUpdateReturnsNotFoundWhenDbReturnsNull() { // Arrange var drugGroupId = 1; var adminDrugGroupsService = new Mock <IAdminDrugGroupsService>(); adminDrugGroupsService .Setup(s => s.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(() => null); var controller = new DrugGroupsController(adminDrugGroupsService.Object, null); // Act var result = await controller.Update(drugGroupId); // Assert result.Should().NotBeNull(); result.As <NotFoundResult>().StatusCode.Should().Be(WebConstants.StatusCodeNotFound); }
public async Task PostUpdateShouldReturnsViewWithModelWhenModelNameExists() { // Arrange var resultDrugGroup = new DrugGroup(); var drugGroupFormModel = DataHelper.GetDrugGroupFormModel(); var adminDrugGroupsService = new Mock <IAdminDrugGroupsService>(); adminDrugGroupsService .Setup(s => s.UpdateAsync(It.IsAny <DrugGroup>())) .Callback((DrugGroup model) => { resultDrugGroup = model; }) .ReturnsAsync(false); var controller = new DrugGroupsController(adminDrugGroupsService.Object, null); // Act var result = await controller.Update(drugGroupFormModel); // Assert result.Should().NotBeNull(); controller.ModelState[WebConstants.StatusMessage].Errors[0].ErrorMessage.Should().Be(WebConstants.DrugGroupNameExists); }
public async Task PostUpdateShouldReturnRedirectWithValidModel() { // Arrange var resultDrugGroup = new DrugGroup(); string successMessage = null; var drugGroupFormModel = DataHelper.GetDrugGroupFormModel(); var adminDrugGroupsService = new Mock <IAdminDrugGroupsService>(); adminDrugGroupsService .Setup(s => s.UpdateAsync(It.IsAny <DrugGroup>())) .Callback((DrugGroup model) => { resultDrugGroup = model; }) .ReturnsAsync(true); var tempData = new Mock <ITempDataDictionary>(); tempData .SetupSet(t => t[WebConstants.TempDataSuccessMessageKey] = It.IsAny <string>()) .Callback((string key, object message) => successMessage = message as string); var controller = new DrugGroupsController(adminDrugGroupsService.Object, null); controller.TempData = tempData.Object; // Act var result = await controller.Update(drugGroupFormModel); // Assert resultDrugGroup.Should().NotBeNull(); resultDrugGroup.Name.Should().Be(drugGroupFormModel.Name); resultDrugGroup.AdminId.Should().Be(drugGroupFormModel.AdminId); successMessage.Should().Be($"Drug group {drugGroupFormModel.Name} successfully updated."); result.Should().BeOfType <RedirectToActionResult>(); result.As <RedirectToActionResult>().ActionName.Should().Be("Index"); }