Esempio n. 1
0
        public void GetAllTasks_MockDbSetTasksAndContext_AndGetAllTasks()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();

            User user1 = DbSetTools.CreateUser(1, miniAutoFixture);
            User user2 = DbSetTools.CreateUser(1, miniAutoFixture);

            List <Task> tasksCollection = DbSetTools.CreateTaskList(1, user1, 10, miniAutoFixture)
                                          .Concat(DbSetTools.CreateTaskList(11, user2, 20, miniAutoFixture))
                                          .ToList();

            IQueryable <Task> tasks = tasksCollection.AsQueryable();

            var mockDbSet = new Mock <DbSet <Task> >();

            mockDbSet.As <IQueryable <Task> >().Setup(m => m.Provider).Returns(tasks.Provider);
            mockDbSet.As <IQueryable <Task> >().Setup(m => m.Expression).Returns(tasks.Expression);
            mockDbSet.As <IQueryable <Task> >().Setup(m => m.ElementType).Returns(tasks.ElementType);
            mockDbSet.As <IQueryable <Task> >().Setup(m => m.GetEnumerator()).Returns(tasks.GetEnumerator());

            var mockContext = new Mock <OrganizerContext>();

            mockContext.Setup(c => c.Tasks).Returns(mockDbSet.Object);
            var            logger         = new Mock <ILogger <ViewRepository> >();
            ViewRepository viewRepository = new ViewRepository(mockContext.Object, logger.Object);

            //action
            IEnumerable <TaskView> taskList = viewRepository.GetAllTasks();

            //assert
            Assert.Equal(tasks.Count(), taskList.Count());
            tasks.ToList().ForEach(task =>
            {
                Assert.Equal
                (
                    taskList.Count
                    (
                        taskView =>
                        taskView.Id == task.Id &&
                        taskView.Text == task.Text &&
                        taskView.Date == task.Date &&
                        taskView.Deleted == task.Deleted &&
                        taskView.Done == task.Done
                    ),
                    1
                );
            }
                                   );
        }