public async Task <BrandUpdatedDto> UpdateBrand(Brand brand) { var brandUpdatedDto = new BrandUpdatedDto(); var existingBrand = await context.Brands.FirstOrDefaultAsync(x => x.Id == brand.Id); try { context.Brands.Update(brand); context.SaveChanges(); brandUpdatedDto.IsSuccess = true; } catch (Exception ex) { Console.WriteLine(ex); brandUpdatedDto.Exception = ex; } return(brandUpdatedDto); }
public async Task Test_EditBrand_ThrowsException() { //arrange var brandForEdit = new Brand { Id = 1, Name = "Test Brand" }; var resultModel = new BrandUpdatedDto { IsSuccess = false, Exception = new Exception() }; brandRepositoryMock.Setup(p => p.UpdateBrand(brandForEdit)).Returns(Task.FromResult(resultModel)); //act var result = await subject.EditBrand(brandForEdit); var badRequest = result as BadRequestObjectResult; //assert Assert.IsInstanceOf <BadRequestObjectResult>(badRequest); brandRepositoryMock.Verify(x => x.UpdateBrand(brandForEdit), Times.Once); }
public async Task Test_EditBrand_ShouldEditSuccessfully() { //arrange var brandForEdit = new Brand { Id = 1, Name = "Test Brand" }; var resultModel = new BrandUpdatedDto() { IsSuccess = true }; brandRepositoryMock.Setup(p => p.UpdateBrand(brandForEdit)).Returns(Task.FromResult(resultModel)); //act var result = await subject.EditBrand(brandForEdit); var OkActionResult = result as OkResult; //assert Assert.IsInstanceOf <OkResult>(OkActionResult); brandRepositoryMock.Verify(x => x.UpdateBrand(brandForEdit), Times.Once); }