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 PutCategoryIncome_ShouldBeAbleToReturnFalseWhenGetAsyncReturnNull() { // Arrange mockRepo.Setup(y => y.GetAsync(It.IsAny <int>())) .Returns(Task.FromResult((Entities.CategoryIncome)null)); var sut = new CategoryIncomeService(mockRepo.Object, null); // Act var resultOfEditCategoryIncome = await sut.EditCategoryIncome(categoryIncomeModelObj, 1); // Assert Assert.IsFalse(resultOfEditCategoryIncome, "GetAsync should return null."); mockRepo.Verify(x => x.GetAsync(It.IsAny <int>()), Times.Once, "GetAsync should run once"); }
public async Task PutCategoryIncome_ShouldBeAbleToReturnTrueWhenSaveAsyncSuccessful() { // 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); // Act var resultOfEditCategoryIncome = await sut.EditCategoryIncome(categoryIncomeModelObj, 1); // Assert Assert.IsTrue(resultOfEditCategoryIncome, "SaveAsync should successful when edit CategoryIncome."); mockRepo.Verify( x => x.GetAsync(1), Times.Once, "GetAsync should run once"); mockRepo.Verify( x => x.SaveAsync(), Times.Once, "SaveAsync should run once"); }