public async Task <IActionResult> DeleteBrand(BrandForDeleteDto brandForDelete) { var result = await repo.DeleteBrand(brandForDelete); if (result.IsSuccess) { return(Ok()); } else { return(BadRequest(result.Exception.GetType() == typeof(ArgumentNullException) ? "No item with this id found" : "Failed to delete item")); } }
public async Task <BrandDeletedDto> DeleteBrand(BrandForDeleteDto brandForDelete) { var brandDeletedDto = new BrandDeletedDto(); try { var brandToDelete = await context.Brands.FirstOrDefaultAsync(x => x.Id == brandForDelete.Id); context.Brands.Remove(brandToDelete); context.SaveChanges(); brandDeletedDto.IsSuccess = true; } catch (Exception ex) { brandDeletedDto.Exception = ex; } return(brandDeletedDto); }
public async Task Test_DeleteBrand_ShouldDeleteSuccessfully() { //arrange var brandForDelete = new BrandForDeleteDto { Id = 1 }; var resultModel = new BrandDeletedDto { IsSuccess = true }; brandRepositoryMock.Setup(p => p.DeleteBrand(brandForDelete)).Returns(Task.FromResult(resultModel)); //act var result = await subject.DeleteBrand(brandForDelete); var OkActionResult = result as OkResult; //assert Assert.IsInstanceOf <OkResult>(OkActionResult); brandRepositoryMock.Verify(x => x.DeleteBrand(brandForDelete), Times.Once); }
public async Task Test_DeleteBrand_ThrowsException() { //arrange var brandForDelete = new BrandForDeleteDto { Id = 1 }; var resultModel = new BrandDeletedDto { IsSuccess = false, Exception = new Exception() }; brandRepositoryMock.Setup(p => p.DeleteBrand(brandForDelete)).Returns(Task.FromResult(resultModel)); //act var result = await subject.DeleteBrand(brandForDelete); var badRequest = result as BadRequestObjectResult; //assert Assert.IsInstanceOf <BadRequestObjectResult>(badRequest); brandRepositoryMock.Verify(x => x.DeleteBrand(brandForDelete), Times.Once); }