コード例 #1
0
        public async Task CategoryExpensePut_TryingChangeEditingObjectInDB_ShouldBeAbleEditObjectAndSaveChangesToDatabase()
        {
            // Arrange
            mockRepo.Setup(y => y.GetAsync(It.IsAny <int>()))
            .Returns(Task.FromResult(categoryExpenseEntityObj));
            mockRepo.Setup(y => y.SaveAsync())
            .Returns(() => Task.Run(() => { return(true); })).Verifiable();

            var sut = new CategoryExpenseService(mockRepo.Object, null);

            categoryExpenseModelObj = new Models.CategoryExpense {
                Id = 2, Description = "x2 Changed"
            };

            var entityFromDB = queryDBInMemory.GetAsync(2);

            entityFromDB.Result.Description = categoryExpenseModelObj.Description;
            await context.SaveChangesAsync();

            var isUpdatedNewObject = queryDBInMemory.GetAsync(2);
            // Act
            var resultOfEditCategoryExpense = await sut.EditCategoryExpense(categoryExpenseModelObj, 2);

            // Assert
            Assert.IsTrue(resultOfEditCategoryExpense, "SaveAsync should successful when edit CategoryExpense.");
            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");
        }
コード例 #2
0
        public async Task PutCategoryExpense_ShouldBeAbleToReturnFalseWhenGetAsyncReturnNull()
        {
            // Arrange
            mockRepo.Setup(y => y.GetAsync(It.IsAny <int>()))
            .Returns(Task.FromResult((Entities.CategoryExpense)null));
            var sut = new CategoryExpenseService(mockRepo.Object, null);

            // Act
            var resultOfEditCategoryExpense = await sut.EditCategoryExpense(categoryExpenseModelObj, 1);

            // Assert
            Assert.IsFalse(resultOfEditCategoryExpense, "GetAsync should return null.");
            mockRepo.Verify(x => x.GetAsync(It.IsAny <int>()), Times.Once, "GetAsync should run once");
        }
コード例 #3
0
        public async Task PutCategoryExpense_ShouldBeAbleToReturnTrueWhenSaveAsyncSuccessful()
        {
            // Arrange
            mockRepo.Setup(y => y.GetAsync(It.IsAny <int>()))
            .Returns(Task.FromResult(categoryExpenseEntityObj));
            mockRepo.Setup(y => y.SaveAsync())
            .Returns(() => Task.Run(() => { return(true); })).Verifiable();

            var sut = new CategoryExpenseService(mockRepo.Object, null);

            // Act
            var resultOfEditCategoryExpense = await sut.EditCategoryExpense(categoryExpenseModelObj, 1);

            // Assert
            Assert.IsTrue(resultOfEditCategoryExpense, "SaveAsync should successful when edit CategoryExpense.");
            mockRepo.Verify(
                x => x.GetAsync(1), Times.Once, "GetAsync should run once");
            mockRepo.Verify(
                x => x.SaveAsync(), Times.Once, "SaveAsync should run once");
        }