public async Task CategoryIncomeAdd_TryingAddNewObjectToDB_ShouldBeAbleReturnIdEquals8() { // Arrange mockMapper.Setup(x => x.Map <List <Entities.CategoryIncome> >(It.IsAny <List <Models.CategoryIncome> >())) .Returns(It.IsAny <List <Entities.CategoryIncome> >()); mockRepo.Setup(y => y.AddRangeAsync(It.IsAny <IEnumerable <Entities.CategoryIncome> >())) .Returns(() => Task.Run(() => { return(true); })).Verifiable(); mockRepo.Setup(y => y.SaveAsync()) .Returns(() => Task.Run(() => { return(true); })).Verifiable(); categoryIncomeEntityLists = new List <Entities.CategoryIncome> { new Entities.CategoryIncome { Id = 8, Description = "New category Expense was added" } }; var sut = new CategoryIncomeService(mockRepo.Object, mockMapper.Object); // Act var resultOfAddCategoryIncome = await sut.AddCategoryIncome(categoryIncomeModelLists); await context.CategoryIncomes.AddRangeAsync(categoryIncomeEntityLists); await context.SaveChangesAsync(); var isAddedNewObject = queryDBInMemory.GetAsync(8); // Assert Assert.AreEqual(8, isAddedNewObject.Result.Id, "New object was not added, require id=8"); Assert.IsTrue(resultOfAddCategoryIncome, "Add and Save should return true. Object i added to Database"); mockRepo.Verify( x => x.AddRangeAsync(It.IsAny <IEnumerable <Entities.CategoryIncome> >()), Times.Once, "AddRangeAsync should run once"); mockRepo.Verify( x => x.SaveAsync(), Times.Once, "SaveAsync should run once"); }
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 RemoveCategoryIncome_ShouldBeAbleToRemoveOneObjectWithIdEquals1() { // Arrange categoryIncomeEntityObj = new Entities.CategoryIncome { Id = 1, Description = "description1" }; mockRepo.Setup(y => y.GetAsync(It.IsAny <int>())) .Returns(Task.FromResult(categoryIncomeEntityObj)); mockRepo.Setup(y => y.RemoveAsync(It.IsAny <Entities.CategoryIncome>())) .Returns(Task.FromResult(true)); var sut = new CategoryIncomeService(mockRepo.Object, null); var getEntity = await sut.GetAsync(1); // Act var isRemoved = await sut.RemoveAsync(getEntity); // Assert Assert.AreEqual(1, getEntity.Id, "GetAsync doesn't return correct Object"); Assert.IsTrue(isRemoved, "RemoveAsync doesn't removed correct Object"); mockRepo.Verify(x => x.GetAsync(1), Times.Once, "GetAsync should run once"); mockRepo.Verify(x => x.RemoveAsync(getEntity), Times.Once, "RemoveAsync should run once"); }
public async Task GetAllCategoryIncome_ShouldBeAbleToReturnTwoObjects() { // Arrange categoryIncomeEntityLists = new List <Entities.CategoryIncome>() { new Entities.CategoryIncome { Id = 8, Description = "description8" }, new Entities.CategoryIncome { Id = 9, Description = "description9" } }.AsEnumerable(); mockRepo.Setup(y => y.GetAllAsync()) .Returns(Task.FromResult(categoryIncomeEntityLists)); var sut = new CategoryIncomeService(mockRepo.Object, null); // Act var getAllEntities = await sut.GetAllAsync(); // Assert Assert.AreEqual(2, getAllEntities.Count(), "GetAll doesn't return correct Count"); mockRepo.Verify(x => x.GetAllAsync(), Times.Once, "GetAllAsync 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 AddCategoryIncome_ShouldRunAddRangeAsyncOnlyOnce() { // Arrange mockMapper.Setup(x => x.Map <List <Entities.CategoryIncome> >(It.IsAny <List <Models.CategoryIncome> >())) .Returns(It.IsAny <List <Entities.CategoryIncome> >()); mockRepo.Setup(y => y.AddRangeAsync(It.IsAny <IEnumerable <Entities.CategoryIncome> >())) .Returns(() => Task.Run(() => { })).Verifiable(); var sut = new CategoryIncomeService(mockRepo.Object, mockMapper.Object); // Act await sut.AddCategoryIncome(categoryIncomeModelLists); // Assert mockRepo.Verify( x => x.AddRangeAsync(It.IsAny <IEnumerable <Entities.CategoryIncome> >()), Times.Once, "AddRangeAsync should run once"); }
public async Task GetCategoryIncome_ShouldBeAbleToReturnOneObjectWithIdEquals8() { // Arrange categoryIncomeEntityObj = new Entities.CategoryIncome { Id = 8, Description = "description8" }; mockRepo.Setup(y => y.GetAsync(It.IsAny <int>())) .Returns(Task.FromResult(categoryIncomeEntityObj)); var sut = new CategoryIncomeService(mockRepo.Object, null); // Act var getAllEntities = await sut.GetAsync(8); // Assert Assert.AreEqual(8, getAllEntities.Id, "GetAsync doesn't return correct Object"); mockRepo.Verify(x => x.GetAsync(8), 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"); }
public async Task AddCategoryIncome_ShouldNotBeAbleToAddCategoryIncome() { // Arrange mockMapper.Setup(x => x.Map <List <Entities.CategoryIncome> >(It.IsAny <List <Models.CategoryIncome> >())) .Returns(It.IsAny <List <Entities.CategoryIncome> >()); mockRepo.Setup(y => y.AddRangeAsync(It.IsAny <IEnumerable <Entities.CategoryIncome> >())) .Returns(() => Task.Run(() => { return(true); })).Verifiable(); mockRepo.Setup(y => y.SaveAsync()) .Returns(() => Task.Run(() => { return(false); })).Verifiable(); var sut = new CategoryIncomeService(mockRepo.Object, mockMapper.Object); // Act var resultOfAddCategoryIncome = await sut.AddCategoryIncome(categoryIncomeModelLists); // Assert Assert.IsFalse(resultOfAddCategoryIncome, "Save should return false"); mockRepo.Verify( x => x.AddRangeAsync(It.IsAny <IEnumerable <Entities.CategoryIncome> >()), Times.Once, "AddRangeAsync should run once"); mockRepo.Verify( x => x.SaveAsync(), Times.Once, "SaveAsync should run once"); }