public async Task GetUpdateShouldReturnViewWithValidModel() { // Arrange var brandDb = DataHelper.GetBrand(); var adminBrandsService = new Mock <IAdminBrandsService>(); adminBrandsService .Setup(s => s.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(brandDb); var controller = new BrandsController(adminBrandsService.Object, null); // Act var result = await controller.Update(brandDb.Id); // Assert result.Should().NotBeNull(); result.Should().BeOfType <ViewResult>(); result.As <ViewResult>().ViewName.Should().BeNull(); var model = result.As <ViewResult>().Model; model.Should().BeOfType <BrandFormModel>(); var formModel = model.As <BrandFormModel>(); formModel.Id.Should().Be(brandDb.Id); formModel.Name.Should().Be(brandDb.Name); formModel.WebSite.Should().Be(brandDb.WebSite); formModel.Admin.Name.Should().Be(brandDb.Admin.Name); formModel.AdminId.Should().Be(brandDb.AdminId); }
public async Task GetUpdateReturnsNotFoundWhenDbReturnsNull() { // Arrange var brandId = 1; var adminBrandsService = new Mock <IAdminBrandsService>(); adminBrandsService .Setup(s => s.GetByIdAsync(It.IsAny <int>())) .ReturnsAsync(() => null); var controller = new BrandsController(adminBrandsService.Object, null); // Act var result = await controller.Update(brandId); // Assert result.Should().NotBeNull(); result.As <NotFoundResult>().StatusCode.Should().Be(WebConstants.StatusCodeNotFound); }
public async Task PostUpdateShouldReturnsViewWithModelWhenModelNameExists() { // Arrange var resultBrand = new Brand(); var brandFormModel = DataHelper.GetBrandFormModel(); var adminBrandsService = new Mock <IAdminBrandsService>(); adminBrandsService .Setup(s => s.UpdateAsync(It.IsAny <Brand>())) .Callback((Brand model) => { resultBrand = model; }) .ReturnsAsync(false); var controller = new BrandsController(adminBrandsService.Object, null); // Act var result = await controller.Update(brandFormModel); // Assert result.Should().NotBeNull(); controller.ModelState[WebConstants.StatusMessage].Errors[0].ErrorMessage.Should().Be(WebConstants.BrandNameExists); }
public async Task PostUpdateShouldReturnRedirectWithValidModel() { // Arrange var resultBrand = new Brand(); string successMessage = null; var brandFormModel = DataHelper.GetBrandFormModel(); var adminBrandsService = new Mock <IAdminBrandsService>(); adminBrandsService .Setup(s => s.UpdateAsync(It.IsAny <Brand>())) .Callback((Brand model) => { resultBrand = 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 BrandsController(adminBrandsService.Object, null); controller.TempData = tempData.Object; // Act var result = await controller.Update(brandFormModel); // Assert resultBrand.Should().NotBeNull(); resultBrand.Name.Should().Be(brandFormModel.Name); resultBrand.WebSite.Should().Be(brandFormModel.WebSite); resultBrand.AdminId.Should().Be(brandFormModel.AdminId); successMessage.Should().Be($"Brand {brandFormModel.Name} successfully updated."); result.Should().BeOfType <RedirectToActionResult>(); result.As <RedirectToActionResult>().ActionName.Should().Be("Index"); }