public async Task CategoryIncomePut_TryingChangeEditingObjectInDB_ShouldBeAbleEditObjectAndSaveChangesToDatabase() { // Arrange mockRepo.Setup(y => y.GetAsync(It.IsAny <int>())) .Returns(Task.FromResult(categoryIncomeEntityObj)); mockRepo.Setup(y => y.SaveAsync()) .Returns(() => Task.Run(() => { return(true); })).Verifiable(); var sut = new CategoryIncomeService(mockRepo.Object, null); categoryIncomeModelObj = new Models.CategoryIncome { Id = 2, Description = "x2 Changed" }; var entityFromDB = queryDBInMemory.GetAsync(2); entityFromDB.Result.Description = categoryIncomeModelObj.Description; await context.SaveChangesAsync(); var isUpdatedNewObject = queryDBInMemory.GetAsync(2); // Act var resultOfEditCategoryIncome = await sut.EditCategoryIncome(categoryIncomeModelObj, 2); // Assert Assert.IsTrue(resultOfEditCategoryIncome, "SaveAsync should successful when edit CategoryIncome."); Assert.AreEqual(2, isUpdatedNewObject.Result.Id, "Object was not updated, require id=2"); Assert.AreEqual("x2 Changed", isUpdatedNewObject.Result.Description, "Object was not updated, require Description=x2 Changed"); mockRepo.Verify( x => x.GetAsync(It.IsAny <int>()), Times.Once, "GetAsync should run once"); mockRepo.Verify( x => x.SaveAsync(), Times.Once, "SaveAsync should run once"); }
public async Task <IActionResult> Edit(int id, [FromBody] Models.CategoryIncome categoryIncomeRequest) { try { if (categoryIncomeRequest == null) { return(BadRequest("Object cannot be null")); } // Update entity in repository var isUpdated = await _categoryIncomeService.EditCategoryIncome(categoryIncomeRequest, id); if (isUpdated) { return(NoContent()); } else { // _logger.LogError($"Add User is not valid. Error in SaveAsync(). When accessing to UserController/Post"); return(StatusCode(500, "A problem happend while handling your request.")); } } catch (Exception ex) { //response.DidError = true; //response.ErrorMessage = "There was an internal error, please contact to technical support."; // Logger?.LogCritical("There was an error on '{0}' invocation: {1}", nameof(PutStockItemAsync), ex); } return(NoContent()); }
public async Task <bool> EditCategoryIncome(Models.CategoryIncome categoryIncomeRequest, int id) { // Get stock item by id var categoryIncomeFromDB = await _categoryIncomeRepository.GetAsync(id); // Validate if entity exists if (categoryIncomeFromDB == null) { return(false); } // Set changes to entity //TODO, check this, search better approach... if (categoryIncomeRequest != null && categoryIncomeRequest.Description != null && categoryIncomeRequest.Description.Trim().Length != 0) { categoryIncomeFromDB.Description = categoryIncomeRequest.Description; } if (!await _categoryIncomeRepository.SaveAsync()) { // _logger.LogError($"Add User is not valid. Error in SaveAsync(). When accessing to UserController/Post"); return(false); } return(true); }
public async Task Setup() { await InitDatabaseInMemoryTest(); categoryIncomeModelLists = new List <Models.CategoryIncome>(); categoryIncomeEntityLists = new List <Entities.CategoryIncome>(); mockRepo = new Mock <ICategoryIncomeRepository>(); mockMapper = new Mock <IMapper>(); categoryIncomeEntityObj = new Entities.CategoryIncome { Id = 2, Description = "CategoryIncome1", IsDeleted = false, Weight = 2, CategoryGroupId = 2 }; categoryIncomeModelObj = new Models.CategoryIncome { Id = 2, Description = "CategoryIncome2", IsDeleted = false, Weight = 2, CategoryGroupId = 2 }; categoryIncomeModelLists.Add(categoryIncomeModelObj); }
public void Setup() { categoryIncomeObj = new Entities.CategoryIncome { Id = 2, Description = "CategoryIncome2", IsDeleted = false, Weight = 2, CategoryGroupId = 2 }; categoryIncomeModelObj = new Models.CategoryIncome { Id = 2, Description = "CategoryIncome2", IsDeleted = false, Weight = 2, CategoryGroupId = 2 }; mockCategoryIncomeRepository = new Mock <ICategoryIncomeRepository>(); mockCategoryIncomeService = new Mock <ICategoryIncomeService>(); expectedIdOfCategoryIncome = 2; categoryIncomeListObj = new List <Models.CategoryIncome>() { new Models.CategoryIncome { Id = 2, Description = "CategoryIncome2", IsDeleted = false, Weight = 2, CategoryGroupId = 2 } }; }