public void GetById_Test() { ParentTask pTask = new ParentTask(); pTask.ParentId = 1; pTask.TaskName = "ABCD"; var ptasks = new List <ParentTask>(); ptasks.Add(pTask); var t = ptasks.AsQueryable(); //var mockSet = new Mock<DbSet<Task_Table>>(); var mockSet = new Mock <DbSet <ParentTask> >(); mockSet.As <IQueryable <ParentTask> >().Setup(m => m.Provider).Returns(t.Provider); mockSet.As <IQueryable <ParentTask> >().Setup(m => m.Expression).Returns(t.Expression); mockSet.As <IQueryable <ParentTask> >().Setup(m => m.ElementType).Returns(t.ElementType); mockSet.As <IQueryable <ParentTask> >().Setup(m => m.GetEnumerator()).Returns(t.GetEnumerator()); var mockTaskMangerContext = new Mock <ProjectmanagerContext>(); mockTaskMangerContext.Setup(c => c.ParentTasks).Returns(mockSet.Object); mockTaskMangerContext.Setup(c => c.Set <ParentTask>()).Returns(mockSet.Object); mockTaskMangerContext.Setup(c => c.Set <ParentTask>().Find(1)).Returns(pTask); mockTaskMangerContext.Setup(p => p.SaveChanges()); var objTestTaskrepo = new ProjectManagerRepository <ParentTask>(mockTaskMangerContext.Object); var retObj = objTestTaskrepo.GetById(1); Assert.AreEqual(retObj.TaskName, pTask.TaskName); mockTaskMangerContext.Verify(mock => mock.Set <ParentTask>().Find(1), Times.Once()); }