示例#1
0
        public void PutTaskRunServerAddUserListAndTask_ReturnTaskAdd()
        {
            DoIntegrationTest(async(client, dbContextOptions) =>
            {
                //Arrange
                MiniAutoFixture miniAutoFixture = new MiniAutoFixture();
                miniAutoFixture.Initialize();
                List <User> userList  = null;
                List <Task> taskList  = DbSetTools.AddExampleTasksToDatabase(dbContextOptions, miniAutoFixture, out userList);
                Task taskExample      = taskList[0];
                TaskModify taskModify = DomainTools.CreateTaskModify(miniAutoFixture);

                //Action
                var response = await client.PutAsJsonAsync(string.Format("/api/task/{0}", taskExample.Id), taskModify);

                //Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                var content       = await response.Content.ReadAsStringAsync();
                TaskView taskView = JsonConvert.DeserializeObject <TaskView>(content);
                Assert.Equal(taskModify.Text, taskView.Text);
                Assert.Equal(taskModify.Date, taskView.Date);
                Assert.Equal(taskExample.Deleted, taskView.Deleted);
                Assert.Equal(taskExample.Done, taskView.Done);
                Assert.Equal(taskExample.Id, taskView.Id);
            });
        }
示例#2
0
        public void Post_MockModifyRepository_AndInsertTask()
        {
            //assign
            MiniAutoFixture miniAutoFixture = new MiniAutoFixture();

            miniAutoFixture.Initialize();

            int idUser           = miniAutoFixture.CreateInt();
            var viewRepository   = new Mock <IViewRepository>();
            var modifyRepository = new Mock <IModifyRepository>();
            var logger           = new Mock <ILogger <TaskController> >();

            TaskModify taskModify = DomainTools.CreateTaskModify(miniAutoFixture);
            TaskView   taskView   = new TaskView
            {
                Id      = miniAutoFixture.CreateInt(),
                Text    = taskModify.Text,
                Date    = taskModify.Date,
                Deleted = false,
                Done    = false
            };

            modifyRepository.Setup(a => a.InsertTask(It.IsAny <int>(), It.IsAny <TaskModify>())).Returns <int, TaskModify>((a, b) => taskView);

            TaskController taskController = new TaskController(viewRepository.Object, modifyRepository.Object, logger.Object);

            //action
            IActionResult actionResult = taskController.Post(idUser, taskModify);

            //assert
            var okResult   = Assert.IsType <OkObjectResult>(actionResult);
            var returnTask = Assert.IsType <TaskView>(okResult.Value);

            modifyRepository.Verify(a => a.InsertTask(It.Is <int>(val => val == idUser), It.Is <TaskModify>(val => val == taskModify)), Times.Once);
            Assert.NotNull(returnTask);
            Assert.Equal(taskView.Id, returnTask.Id);
            Assert.Equal(taskModify.Text, returnTask.Text);
            Assert.Equal(taskModify.Date, returnTask.Date);
            Assert.Equal(taskView.Deleted, returnTask.Deleted);
            Assert.Equal(taskView.Done, returnTask.Done);
        }
        public void UpdateTask_MockDbSetTask_AndGetUpdateTask()
        {
            //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);
            TaskModify       taskModify       = DomainTools.CreateTaskModify(miniAutoFixture);

            //action
            TaskView taskView = modifyRepository.UpdateTask(idTaskModify, taskModify);

            //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(taskModify.Date, taskView.Date);
            Assert.Equal(taskModify.Text, taskView.Text);
            Assert.Equal(taskModified.Deleted, taskView.Deleted);
            Assert.Equal(taskModified.Done, taskView.Done);
            Assert.Equal(idTaskModify, taskView.Id);
        }
        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);
        }