public async Task TestGet_VerifyTaskName() { SetUpMockData(); var taskRepository = new TaskManagerRepository(mockContext.Object); var taskDetails = await taskRepository.GetAsync(2); Assert.IsType <TaskItem>(taskDetails); }
public async Task TestGet_VerifyTaskName() { var contextOptions = new DbContextOptions <TaskManagerDbContext>(); var mockContext = new Mock <TaskManagerDbContext>(contextOptions); var taskRepository = new TaskManagerRepository(mockContext.Object); IQueryable <TaskItem> taskDetailsList = new List <TaskItem>() { new TaskItem() { Id = 1, Name = "Task 1", Priority = 10 }, new TaskItem() { Id = 2, Name = "Task 2", Priority = 20 }, }.AsQueryable(); var mockSet = new Mock <DbSet <TaskItem> >(); mockSet.As <IAsyncEnumerable <TaskItem> >() .Setup(m => m.GetEnumerator()) .Returns(new TestAsyncEnumerator <TaskItem>(taskDetailsList.GetEnumerator())); mockSet.As <IQueryable <TaskItem> >() .Setup(m => m.Provider) .Returns(new TestAsyncQueryProvider <TaskItem>(taskDetailsList.Provider)); mockSet.As <IQueryable <TaskItem> >().Setup(m => m.Expression).Returns(taskDetailsList.Expression); mockSet.As <IQueryable <TaskItem> >().Setup(m => m.ElementType).Returns(taskDetailsList.ElementType); mockSet.As <IQueryable <TaskItem> >().Setup(m => m.GetEnumerator()).Returns(() => taskDetailsList.GetEnumerator()); mockContext.Setup(m => m.Tasks).Returns(mockSet.Object); var taskDetails = await taskRepository.GetAsync(2); Assert.Equal("Task 2", taskDetails.Name); }