public async Task Edit_ProductIdIsLessOrEqualZero_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); var editProductGroup = new ProductGroupAddDTO { }; bool expectEx1 = false; bool expectEx2 = false; // ===== Act ===== var service = new ProductGroupServices(context, mapper, httpContext.Object); try { await service.Edit(0, editProductGroup); } catch (ArgumentOutOfRangeException) { expectEx1 = true; } try { await service.Edit(-10, editProductGroup); } catch (ArgumentOutOfRangeException) { expectEx2 = true; } // ===== Assert ===== // Result 1 : When Id = 0 Assert.IsTrue(expectEx1); // Result 1 : When Id = -10 Assert.IsTrue(expectEx2); }
public async Task Edit_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); var editProductGroup = new ProductGroupAddDTO { Name = "Test Product Group" }; // ===== Act ===== var service = new ProductGroupServices(context, mapper, httpContext.Object); var result = await service.Edit(1, editProductGroup); // ===== Assert ===== // No data in database must return an error message // Expected Exception }
public async Task Edit_SentBlankProductGroupName_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); var productGroupId = 1; var editProductGroup = new ProductGroupAddDTO { }; // ===== Act ===== var service = new ProductGroupServices(context, mapper, httpContext.Object); var result = await service.Edit(productGroupId, editProductGroup); // ===== Assert ===== // Except Exception }
public async Task Edit_WithData_ReturnEditedResult() { // ===== 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); var productGroupId1 = 1; var productGroupId2 = 3; var productGroupId3 = 99; var editProductGroup1 = new ProductGroupAddDTO { Name = "Test Product Group Edit 1" }; var editProductGroup2 = new ProductGroupAddDTO { Name = "Test Product Group Edit 2" }; var editProductGroup3 = new ProductGroupAddDTO { Name = "Test Product Group Edit 3" }; bool expectEx1 = false; bool expectEx2 = false; bool expectEx3 = false; var result1 = new ServiceResponse <ProductGroupDTO>(); var result2 = new ServiceResponse <ProductGroupDTO>(); var result3 = new ServiceResponse <ProductGroupDTO>(); // 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(); // ===== Act ===== var actContext = BuildContext(dbName); var service = new ProductGroupServices(actContext, mapper, httpContext.Object); try { result1 = await service.Edit(productGroupId1, editProductGroup1); } catch (Exception) { expectEx1 = true; throw; } try { result2 = await service.Edit(productGroupId2, editProductGroup2); } catch (Exception) { expectEx2 = true; throw; } try { result3 = await service.Edit(productGroupId3, editProductGroup3); } catch (InvalidOperationException) { expectEx3 = true; } // ===== Assert ===== var assContext = BuildContext(dbName); 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 : Edit ProductGroup (ID 1) Must be changed Assert.IsTrue(result1.IsSuccess); Assert.AreEqual(result1.Message, $"Product Group ({editProductGroup1.Name}) have been edited successfully"); Assert.IsNotNull(chkProductGroup1); Assert.AreEqual(result1.Data.Id, productGroupId1); Assert.AreEqual(result1.Data.Id, chkProductGroup1.Id); Assert.AreNotEqual(result1.Data.Name, dataProductGroup1.Name); Assert.AreEqual(result1.Data.Name, chkProductGroup1.Name); Assert.IsFalse(expectEx1); // Result 2 : Edit ProductGroup (ID 3) Must be changed Assert.IsTrue(result2.IsSuccess); Assert.AreEqual(result2.Message, $"Product Group ({editProductGroup2.Name}) have been edited successfully"); Assert.IsNotNull(chkProductGroup2); Assert.AreEqual(result2.Data.Id, productGroupId2); Assert.AreEqual(result2.Data.Id, chkProductGroup2.Id); Assert.AreNotEqual(result2.Data.Name, dataProductGroup2.Name); Assert.AreEqual(result2.Data.Name, chkProductGroup2.Name); Assert.IsFalse(expectEx2); // Result 3 : Edit ProductGroup (ID 99) Must be return an error message Assert.IsNull(result3.Data); Assert.IsTrue(expectEx3); Assert.IsNull(dataProductGroup3); Assert.IsNull(chkProductGroup3); }