Exemple #1
0
        public async Task DeletePoint_Should_DeletePoint()
        {
            //Setup DbContext and DbSet mock
            var dbContextMock = new Mock <ITodoContext>();

            var testTodoPointId = 1;

            var testPoints = GetTestTodoPoints();

            var todoPointDbSetMock = testPoints.AsQueryable().GetMockDbSet();

            dbContextMock.Setup(x => x.DbSet <TodoPoint>()).Returns(todoPointDbSetMock.Object);
            todoPointDbSetMock.Setup(x => x.Remove(testPoints.FirstOrDefault()))
            .Callback(() => testPoints.Clear());

            //Execute
            var todoPointService      = new TodoPointService(dbContextMock.Object);
            var todoPointBeforeDelete = await todoPointService.GetAsync <TodoPoint>(x => x.PointId == testTodoPointId);

            await todoPointService.DeleteAsync(todoPointBeforeDelete);

            var todoPointAfterDelete = await todoPointService.GetAsync <TodoPoint>(x => x.PointId == testTodoPointId);

            //Assert
            Assert.NotNull(todoPointBeforeDelete);
            Assert.Null(todoPointAfterDelete);
            Assert.IsAssignableFrom <IEnumerable <TodoPoint> >(testPoints);
            Assert.NotNull(testPoints);
        }
Exemple #2
0
        public async Task DeletePointsByTodoId_Should_DeletePoints()
        {
            //Setup DbContext and DbSet mock
            var dbContextMock = new Mock <ITodoContext>();

            var testTodoId      = 1;
            var testTodoPointId = 1;

            var testPoints = GetTestTodoPoint(testTodoPointId, testTodoId);
            var testTodos  = GetTestTodo(testTodoId, testPoints);

            var todoDbSetMock      = testTodos.AsQueryable().GetMockDbSet();
            var todoPointDbSetMock = testPoints.AsQueryable().GetMockDbSet();

            dbContextMock.Setup(x => x.DbSet <Todo>()).Returns(todoDbSetMock.Object);
            dbContextMock.Setup(x => x.DbSet <TodoPoint>()).Returns(todoPointDbSetMock.Object);
            todoPointDbSetMock.Setup(x => x.RemoveRange(It.IsAny <List <TodoPoint> >()))
            .Callback(() => testPoints.Clear());

            //Execute
            var todoPointService      = new TodoPointService(dbContextMock.Object);
            var todoPointBeforeDelete = await todoPointService.GetAsync <TodoPoint>(x => x.PointId == testTodoPointId);

            await todoPointService.DeletePointsByTodoId(testTodoId);

            //Assert
            Assert.NotNull(todoPointBeforeDelete);
            Assert.Empty(testPoints);
        }
Exemple #3
0
        public async Task CreateAsync_Should_CreatePoint()
        {
            //Setup DbContext and DbSet mock
            var dbContextMock = new Mock <ITodoContext>();

            var testTodoPoint = new TodoPoint
            {
                PointId     = 3,
                TodoId      = 2,
                Description = "test point 3"
            };

            var testPoints = GetTestTodoPoints();

            var todoPointDbSetMock = testPoints.AsQueryable().GetMockDbSet();

            dbContextMock.Setup(x => x.DbSet <TodoPoint>()).Returns(todoPointDbSetMock.Object);
            todoPointDbSetMock.Setup(x => x.Add(It.IsAny <TodoPoint>()))
            .Callback(() =>
            {
                testPoints.Add(testTodoPoint);
            });
            //Execute
            var todoPointService = new TodoPointService(dbContextMock.Object);
            await todoPointService.CreateAsync(testTodoPoint);

            var todoPoint = await todoPointService.GetAsync <TodoPoint>(x => x.PointId == testTodoPoint.PointId);

            //Assert
            Assert.IsAssignableFrom <TodoPoint>(todoPoint);
            Assert.Equal(testTodoPoint.Description, todoPoint.Description);
            Assert.Equal(3, testPoints.Count);
        }
Exemple #4
0
        public async Task GetAsync_Should_ReturnPoint()
        {
            //Setup DbContext and DbSet mock
            var dbContextMock = new Mock <ITodoContext>();

            var testTodoPointId = 1;

            var testPoints = GetTestTodoPoints();

            var todoPointDbSetMock = testPoints.AsQueryable().GetMockDbSet();

            dbContextMock.Setup(x => x.DbSet <TodoPoint>()).Returns(todoPointDbSetMock.Object);

            //Execute
            var todoPointService = new TodoPointService(dbContextMock.Object);
            var todoPoint        = await todoPointService.GetAsync <TodoPoint>(x => x.PointId == testTodoPointId);

            //Assert
            Assert.IsAssignableFrom <TodoPoint>(todoPoint);
        }
Exemple #5
0
        public async Task UpdateAsync_Should_UpdatePoint()
        {
            //Setup DbContext and DbSet mock
            var dbContextMock = new Mock <ITodoContext>();

            var testTodoPoint = new TodoPoint
            {
                PointId     = 2,
                TodoId      = 1,
                Description = "updated"
            };

            var testPoints = GetTestTodoPoints();

            var    pointBefore      = testPoints.Where(x => x.PointId == testTodoPoint.PointId).FirstOrDefault();
            string pointDescription = pointBefore.Description;

            var todoPointDbSetMock = testPoints.AsQueryable().GetMockDbSet();

            dbContextMock.Setup(x => x.DbSet <TodoPoint>()).Returns(todoPointDbSetMock.Object);
            todoPointDbSetMock.Setup(x => x.Update(It.IsAny <TodoPoint>()))
            .Callback(() =>
            {
                var point         = testPoints.Where(x => x.PointId == testTodoPoint.PointId).FirstOrDefault();
                point.Description = testTodoPoint.Description;
            });
            //Execute
            var todoPointService = new TodoPointService(dbContextMock.Object);
            await todoPointService.UpdateAsync(testTodoPoint);

            var todoPointAfter = await todoPointService.GetAsync <TodoPoint>(x => x.PointId == testTodoPoint.PointId);

            //Assert
            Assert.NotEqual(pointDescription, pointBefore.Description);
            Assert.IsAssignableFrom <TodoPoint>(todoPointAfter);
            Assert.Equal(testTodoPoint.Description, todoPointAfter.Description);
        }