public async Task Add_WithData_ReturnAddedResult() { // ===== 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 addProductGroup = new ProductGroupAddDTO { Name = "Test" }; var user = await SetupUser(context, mapper, httpContext.Object, new UserRegisterDto { Username = "******", Password = "******" }); List <Claim> claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.Name, user.Username) }; var loginHttpContext = new Mock <IHttpContextAccessor>(); var loginHttp = new DefaultHttpContext(); loginHttp.User.AddIdentity(new ClaimsIdentity(claims)); loginHttpContext.Setup(_ => _.HttpContext).Returns(loginHttp); // ===== Act ===== var service = new ProductGroupServices(context, mapper, loginHttpContext.Object); var result = await service.Add(addProductGroup); // ===== Assert ===== // Check that database has new record var resultContext = BuildContext(dbName); var recordCount = await resultContext.ProductGroup.CountAsync(); Assert.AreEqual(recordCount, 1); // Result : Return an error message Assert.IsTrue(result.IsSuccess); Assert.AreEqual(result.Message, $"Product Group ({addProductGroup.Name}) have been added successfully"); }
public async Task Add_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 addProductGroup = new ProductGroupAddDTO { Name = "" }; bool expectEx = false; // ===== Act ===== var service = new ProductGroupServices(context, mapper, httpContext.Object); try { var result = await service.Add(addProductGroup); } catch (ArgumentNullException) { expectEx = true; } // ===== Assert ===== // Check that database has no new record var resultContext = BuildContext(dbName); var recordCount = await resultContext.ProductGroup.CountAsync(); Assert.AreEqual(recordCount, 0); // Result : Return an error message Assert.IsTrue(expectEx); }