Beispiel #1
0
 public void AddTask(EducationObject toAddTask)
 {
     _repo.AddTask(toAddTask);
 }
        public async Task AddTask(int taskId, int parentTaskId, int priority,
                                  string start, string end, int status, string taskDetails)
        {
            var finalRepo = new List <Tasks>();
            var finalParentRepo = new List <ParentTask>();
            var taskList = new List <Tasks> {
                (new Tasks
                {
                    EndDate = (!string.IsNullOrWhiteSpace(end)) ? DateTime.Parse(end) : DateTime.MinValue,
                    StartDate = (!string.IsNullOrWhiteSpace(start)) ? DateTime.Parse(start) : DateTime.MinValue,
                    ParentTask = (parentTaskId > -1) ? new ParentTask {
                        Parent_Task = parentTaskId
                    } : null,
                    Priortiy = priority,
                    Status = status,
                    TaskId = taskId,
                    TaskDeatails = "Test task",
                })
            }.AsQueryable();
            var parTaskList = new List <ParentTask> {
                new ParentTask {
                    Parent_Task = taskId
                }
            }.AsQueryable <ParentTask>();


            var mockDbsetTask    = new Mock <DbSet <Tasks> >();
            var mockDbsetParTask = new Mock <DbSet <ParentTask> >();

            mockDbsetTask.As <IQueryable <Tasks> >().Setup(m => m.Provider).Returns(taskList.Provider);
            mockDbsetTask.As <IQueryable <Tasks> >().Setup(m => m.Expression).Returns(taskList.Expression);
            mockDbsetTask.As <IQueryable <Tasks> >().Setup(m => m.ElementType).Returns(taskList.ElementType);
            mockDbsetTask.As <IQueryable <Tasks> >().Setup(m => m.GetEnumerator()).Returns(taskList.GetEnumerator());
            mockDbsetTask.Setup(ctx => ctx.Add(It.IsAny <Tasks>())).Callback <Tasks>((s) => finalRepo.Add(s));
            mockDbsetParTask.As <IQueryable <ParentTask> >().Setup(m => m.Provider).Returns(parTaskList.Provider);
            mockDbsetParTask.As <IQueryable <ParentTask> >().Setup(m => m.Expression).Returns(parTaskList.Expression);
            mockDbsetParTask.As <IQueryable <ParentTask> >().Setup(m => m.ElementType).Returns(parTaskList.ElementType);
            mockDbsetParTask.As <IQueryable <ParentTask> >().Setup(m => m.GetEnumerator()).Returns(parTaskList.GetEnumerator());
            mockDbsetParTask.Setup(ctx => ctx.Add(It.IsAny <ParentTask>())).Callback <ParentTask>((s) =>
                                                                                                  finalParentRepo.Add(s));
            ////mockDbsetParTask.Setup(ctx => ctx.FirstOrDefault(It.IsAny<Expression<Func<ParentTask, bool>>>()))
            //                      .Returns(new ParentTask
            //                                                  {
            //                                                      Parent_Task = taskId
            //                                                  }
            //                                                );
            DbContextOptions <TaskContext> contextOption = new DbContextOptions <TaskContext>();
            var mockTaskContext = new Mock <TaskContext>(contextOption);

            mockTaskContext.SetupGet(ctx => ctx.Tasks).Returns(mockDbsetTask.Object);
            mockTaskContext.SetupGet(ctx => ctx.ParentTasks).Returns(mockDbsetParTask.Object);
            mockTaskContext.SetupSet(ctx => ctx.Tasks       = mockDbsetTask.Object);
            mockTaskContext.SetupSet(ctx => ctx.ParentTasks = mockDbsetParTask.Object);
            mockTaskContext.Setup(ctx => ctx.SaveChangesAsync(CancellationToken.None))
            .Returns(Task.FromResult(1));

            var repoLogger = new LoggerFactory().CreateLogger <TaskRepo>();
            var taskRepo   = new TaskRepo(mockTaskContext.Object, repoLogger);
            var result     = await taskRepo.AddTask(new Tasks
            {
                EndDate    = (!string.IsNullOrWhiteSpace(end)) ? DateTime.Parse(end) : DateTime.MinValue,
                StartDate  = (!string.IsNullOrWhiteSpace(start)) ? DateTime.Parse(start) : DateTime.MinValue,
                ParentTask = (parentTaskId > -1) ? new ParentTask {
                    Parent_Task = parentTaskId
                } : null,
                Priortiy     = priority,
                Status       = status,
                TaskId       = taskId,
                TaskDeatails = "Test task",
            });

            Assert.True(result);
        }