public async Task Get_HaveDataAndProductID_ReturnProductGroupWithSameId() { // ===== 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 = -5; var productGroupId2 = 1; var productGroupId3 = 9; var productGroupId4 = 4; bool expectEx1 = false; bool expectEx2 = false; bool expectEx3 = false; bool expectEx4 = false; ServiceResponse <ProductGroupDTO> result1 = new ServiceResponse <ProductGroupDTO>(); ServiceResponse <ProductGroupDTO> result2 = new ServiceResponse <ProductGroupDTO>(); ServiceResponse <ProductGroupDTO> result3 = new ServiceResponse <ProductGroupDTO>(); ServiceResponse <ProductGroupDTO> result4 = new ServiceResponse <ProductGroupDTO>(); var actContext = BuildContext(dbName); // ===== Act ===== var service = new ProductGroupServices(actContext, mapper, httpContext.Object); try { result1 = await service.Get(productGroupId1); } catch (ArgumentOutOfRangeException) { expectEx1 = true; } try { result2 = await service.Get(productGroupId2); } catch (Exception) { expectEx2 = true; } try { result3 = await service.Get(productGroupId3); } catch (InvalidOperationException) { expectEx3 = true; } try { result4 = await service.Get(productGroupId4); } catch (Exception) { expectEx4 = true; } // ==== Assert ===== // Result 1 : Id (-5) must be greater than 0, then error Assert.IsNull(result1.Data); Assert.IsTrue(expectEx1); // Result 2 : Have a data & ProductID, It's must return ProductGroup With Same Id Assert.IsTrue(result2.IsSuccess); Assert.IsNotNull(result2.Data); Assert.AreEqual(productGroupId2, result2.Data.Id); Assert.IsFalse(expectEx2); // Result 3 : Have a data but Id is not exist, Return error message Assert.IsNull(result3.Data); Assert.IsTrue(expectEx3); // Result 4 : Have a data & ProductID, It's must return ProductGroup With Same Id Assert.IsTrue(result4.IsSuccess); Assert.IsNotNull(result4.Data); Assert.AreEqual(result4.Data.Id, productGroupId4); Assert.IsFalse(expectEx4); }
public async Task Get_ProductGroupValidation_ReturnError() { // ===== 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 productGroupId1 = -5; var productGroupId2 = 0; var productGroupId3 = 1; bool expectEx1 = false; bool expectEx2 = false; bool expectEx3 = false; // ===== Act ===== var service = new ProductGroupServices(context, mapper, httpContext.Object); try { await service.Get(productGroupId1); } catch (ArgumentOutOfRangeException) { expectEx1 = true; } try { await service.Get(productGroupId2); } catch (ArgumentOutOfRangeException) { expectEx2 = true; } try { await service.Get(productGroupId3); } catch (InvalidOperationException) { expectEx3 = true; } // ==== Assert ===== // Result 1 : Id (-5) must be greater than 0, then error Assert.IsTrue(expectEx1); // Result 2 : Id (0) must be greater than 0, then error Assert.IsTrue(expectEx2); // Result 3 : Id must be greater than 0 but no data in database, then error Assert.IsTrue(expectEx3); }