public async Task Delete_WithID_ReturnDeletedResult() { // ===== Arrange ===== var dbName = Guid.NewGuid().ToString(); var context = BuildContext(dbName); var mapper = BuildMap(); var httpContext = new Mock <IHttpContextAccessor>(); var http = new DefaultHttpContext(); httpContext.Setup(_ => _.HttpContext).Returns(http); await Generate_ProductGroup_Data(context, mapper, httpContext.Object); int productGroupId1 = 1; int productGroupId2 = 4; int productGroupId3 = 99; // Arrange data for later comparison. var dataProductGroup1 = await context.ProductGroup .Where(x => x.Id == productGroupId1) .FirstOrDefaultAsync(); var dataProductGroup2 = await context.ProductGroup .Where(x => x.Id == productGroupId2) .FirstOrDefaultAsync(); var dataProductGroup3 = await context.ProductGroup .Where(x => x.Id == productGroupId3) .FirstOrDefaultAsync(); bool expectEx1 = false; bool expectEx2 = false; bool expectEx3 = false; var result1 = new ServiceResponse <ProductGroupDTO>(); var result2 = new ServiceResponse <ProductGroupDTO>(); var result3 = new ServiceResponse <ProductGroupDTO>(); // ===== Act ===== var actContext = BuildContext(dbName); var service = new ProductGroupServices(actContext, mapper, httpContext.Object); try { result1 = await service.Delete(productGroupId1); } catch (Exception) { expectEx1 = true; throw; } try { result2 = await service.Delete(productGroupId2); } catch (Exception) { expectEx2 = true; throw; } try { result3 = await service.Delete(productGroupId3); } catch (InvalidOperationException) { expectEx3 = true; } catch (Exception) { throw; } // ===== Assert ===== var assContext = BuildContext(dbName); // Arrange data for comparison. var chkProductGroup1 = await assContext.ProductGroup .Where(x => x.Id == productGroupId1) .FirstOrDefaultAsync(); var chkProductGroup2 = await assContext.ProductGroup .Where(x => x.Id == productGroupId2) .FirstOrDefaultAsync(); var chkProductGroup3 = await assContext.ProductGroup .Where(x => x.Id == productGroupId3) .FirstOrDefaultAsync(); // Result 1 : ProductGroup (Id 1) Must be delete and return with Response Assert.IsFalse(expectEx1); Assert.IsTrue(result1.IsSuccess); Assert.AreEqual(result1.Message, $"Product Group ({dataProductGroup1.Name}) have been deleted successfully"); Assert.IsNotNull(dataProductGroup1); Assert.IsNull(chkProductGroup1); // Result 2 : ProductGroup (Id 4) Must be delete and return with Response Assert.IsFalse(expectEx2); Assert.IsTrue(result2.IsSuccess); Assert.AreEqual(result2.Message, $"Product Group ({dataProductGroup2.Name}) have been deleted successfully"); Assert.IsNotNull(dataProductGroup2); Assert.IsNull(chkProductGroup2); // Result 3 : ProductGroup (Id 99) is not in the database, return with Error Message Assert.IsTrue(expectEx3); Assert.IsNull(dataProductGroup3); Assert.IsNull(chkProductGroup3); }
public async Task Delete_NoData_ReturnErrorMessage() { // ===== Arrange ===== var dbName = Guid.NewGuid().ToString(); var context = BuildContext(dbName); var mapper = BuildMap(); var httpContext = new Mock <IHttpContextAccessor>(); var http = new DefaultHttpContext(); httpContext.Setup(_ => _.HttpContext).Returns(http); int productGroupId1 = 4; int productGroupId2 = 0; int productGroupId3 = -21; bool expectEx1 = false; bool expectEx2 = false; bool expectEx3 = false; // ===== Act ===== var result1 = new ServiceResponse <ProductGroupDTO>(); var result2 = new ServiceResponse <ProductGroupDTO>(); var result3 = new ServiceResponse <ProductGroupDTO>(); var service = new ProductGroupServices(context, mapper, httpContext.Object); try { result1 = await service.Delete(productGroupId1); } catch (InvalidOperationException) { expectEx1 = true; } try { result2 = await service.Delete(productGroupId2); } catch (ArgumentOutOfRangeException) { expectEx2 = true; } try { result3 = await service.Delete(productGroupId3); } catch (ArgumentOutOfRangeException) { expectEx3 = true; } // ===== Assert ===== // Result 1 : No data in database must return an error message Assert.IsNull(result1.Data); Assert.IsTrue(expectEx1); // Result 2 : if ID (0) not grater than 0, return an error message Assert.IsNull(result2.Data); Assert.IsTrue(expectEx2); // Result 3 : if ID (-21) not grater than 0, return an error message Assert.IsNull(result3.Data); Assert.IsTrue(expectEx3); }