public void GetAllTasks_MockDbSetTasksAndContext_AndGetAllUserTask() { //assign 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); var logger = new Mock <ILogger <ViewRepository> >(); ViewRepository viewRepository = new ViewRepository(mockContext.Object, logger.Object); //action IEnumerable <TaskView> taskList = viewRepository.GetAllUserTask(userSpecified.Id); //assign IEnumerable <Task> tasksUser = tasks.Where(a => a.User.Id == userSpecified.Id); Assert.Equal(tasksUser.Count(), taskList.Count()); tasksUser.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 ); } ); }