Example #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
                );
            }
                                   );
        }
Example #2
0
        public void MapUserToView_WhenSetUser_ReturnUserView()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            int id = miniAutoFixture.CreateInt();

            User user = DbSetTools.CreateUser(id, miniAutoFixture);

            //action
            UserView userView = ExtensionMethods.MapUserToView(user);

            //assert
            Assert.Equal(user.Id, userView.Id);
            Assert.Equal(user.Name, userView.Name);
            Assert.Equal(user.Surname, userView.Surname);
            Assert.Equal(user.Deleted, userView.Deleted);
        }
Example #3
0
        public void GetAllTasks_MockDbSetTasksAndContext_AndGetTask()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();

            User user          = DbSetTools.CreateUser(11, miniAutoFixture);
            Task taskSpecified = DbSetTools.CreateTask(11, user, miniAutoFixture);

            List <Task> tasksCollection = DbSetTools.CreateTaskList(1, user, 10, miniAutoFixture)
                                          .Concat(new Task[] { taskSpecified })
                                          .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
            TaskView taskView = viewRepository.GetTask(taskSpecified.Id);

            // assign
            Assert.Equal(taskSpecified.Id, taskView.Id);
            Assert.Equal(taskSpecified.Text, taskView.Text);
            Assert.Equal(taskSpecified.Date, taskView.Date);
            Assert.Equal(taskSpecified.Deleted, taskView.Deleted);
            Assert.Equal(taskSpecified.Done, taskView.Done);
        }
        public void DoneTask_MockDbSetTask_AndGetDoneTask()
        {
            //assign
            int             idTaskModify    = 1;
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();
            User              user            = DbSetTools.CreateUser(1, miniAutoFixture);
            List <Task>       tasksCollection = DbSetTools.CreateTaskList(1, user, 10, miniAutoFixture);
            IQueryable <Task> tasks           = tasksCollection.AsQueryable();
            var mockDbSetTask = new Mock <DbSet <Task> >();

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

            var mockContext = new Mock <OrganizerContext>();

            mockContext.Setup(c => c.Tasks).Returns(mockDbSetTask.Object);
            var logger = new Mock <ILogger <ModifyRepository> >();
            ModifyRepository modifyRepository = new ModifyRepository(mockContext.Object, logger.Object);
            bool             done             = miniAutoFixture.CreateBoolean();

            //action
            TaskView taskView = modifyRepository.DoneTask(idTaskModify, done);

            //assert
            Task taskModified = tasksCollection.Where(a => a.Id == idTaskModify).Single();

            mockContext.Verify(a => a.Attach(It.Is <Task>(task => task == taskModified)), Times.Once);
            mockContext.Verify(a => a.SaveChanges(), Times.Once);

            Assert.Equal(taskModified.Date, taskView.Date);
            Assert.Equal(taskModified.Text, taskView.Text);
            Assert.Equal(taskModified.Deleted, taskView.Deleted);
            Assert.Equal(done, taskView.Done);
            Assert.Equal(idTaskModify, taskView.Id);
        }
Example #5
0
        public void GetUser_MockDbSetUserAndContext_AndGetUser()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();

            User        userSpecified   = DbSetTools.CreateUser(101, miniAutoFixture);
            List <User> usersCollection =
                DbSetTools.CreateUserList(1, 100, miniAutoFixture)
                .Concat(new User[] { userSpecified }).ToList();


            IQueryable <User> users = usersCollection.AsQueryable();
            var mockDbSet           = new Mock <DbSet <User> >();

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

            var mockContext = new Mock <OrganizerContext>();

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

            //action
            UserView userView = viewRepository.GetUser(userSpecified.Id);

            //assert
            Assert.Equal(userSpecified.Id, userView.Id);
            Assert.Equal(userSpecified.Name, userView.Name);
            Assert.Equal(userSpecified.Surname, userView.Surname);
            Assert.Equal(userSpecified.Deleted, userView.Deleted);
        }
        public void InsertTask_MockDbSetUserDbSetTask_AndGetInsertTask()
        {
            //assign
            int             newIdTask       = 41;
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();

            User        userSpecified   = DbSetTools.CreateUser(1, miniAutoFixture);
            User        user2           = DbSetTools.CreateUser(2, miniAutoFixture);
            User        user3           = DbSetTools.CreateUser(3, miniAutoFixture);
            List <Task> tasksCollection = DbSetTools.CreateTaskList(1, userSpecified, 20, miniAutoFixture)
                                          .Concat(DbSetTools.CreateTaskList(21, user2, 10, miniAutoFixture))
                                          .Concat(DbSetTools.CreateTaskList(31, user3, 10, miniAutoFixture))
                                          .ToList();

            IQueryable <Task> tasks = tasksCollection.AsQueryable();
            List <User>       userCollection = new User[] { userSpecified, user2, user3 }.ToList();
            IQueryable <User> users = userCollection.AsQueryable();

            var mockDbSetUser = new Mock <DbSet <User> >();
            var mockDbSetTask = new Mock <DbSet <Task> >();

            mockDbSetUser.As <IQueryable <User> >().Setup(m => m.Provider).Returns(users.Provider);
            mockDbSetUser.As <IQueryable <User> >().Setup(m => m.Expression).Returns(users.Expression);
            mockDbSetUser.As <IQueryable <User> >().Setup(m => m.ElementType).Returns(users.ElementType);
            mockDbSetUser.As <IQueryable <User> >().Setup(m => m.GetEnumerator()).Returns(users.GetEnumerator());

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

            var mockContext = new Mock <OrganizerContext>();

            mockContext.Setup(c => c.User).Returns(mockDbSetUser.Object);
            mockContext.Setup(c => c.Tasks).Returns(mockDbSetTask.Object);
            mockDbSetTask.Setup(a => a.Add(It.IsAny <Task>()))
            .Callback <Task>(
                task => {
                task.Id = newIdTask;
                tasksCollection.Add(task);
            });

            var logger = new Mock <ILogger <ModifyRepository> >();
            ModifyRepository modifyRepository = new ModifyRepository(mockContext.Object, logger.Object);
            TaskModify       taskModify       = DomainTools.CreateTaskModify(miniAutoFixture);


            // action
            TaskView taskView = modifyRepository.InsertTask(userSpecified.Id, taskModify);

            //assert
            mockDbSetTask.Verify
            (
                a => a.Add
                (
                    It.Is <Task>
                    (
                        task =>
                        task.Id == newIdTask &&
                        task.Date == task.Date &&
                        !task.Deleted &&
                        !task.Done &&
                        task.Text == taskModify.Text &&
                        task.User == userSpecified
                    )
                ),
                Times.Once
            );

            mockContext.Verify(a => a.SaveChanges(), Times.Once);

            Assert.Equal(taskModify.Text, taskView.Text);
            Assert.Equal(taskModify.Date, taskView.Date);
            Assert.Equal(false, taskView.Done);
            Assert.Equal(false, taskView.Deleted);
            Assert.Equal(newIdTask, taskView.Id);
        }