コード例 #1
0
        public async Task TestUpdateAsync_VerifySaveChangesCalledOnce()
        {
            var contextOptions = new DbContextOptions <TaskManagerDbContext>();
            var mockContext    = new Mock <TaskManagerDbContext>(contextOptions);

            var taskRepository = new TaskManagerRepository(mockContext.Object);

            var taskDetail = new TaskItem()
            {
                Id = 1, Name = "Task 1 ", Priority = 10
            };

            var mockSet = new Mock <DbSet <TaskItem> >();

            mockContext.Setup(m => m.Tasks).Returns(mockSet.Object);
            await taskRepository.UpdateAsync(1, taskDetail);

            mockSet.Verify(m => m.Update(taskDetail), Times.Once);
            mockContext.Verify(m => m.SaveChangesAsync(System.Threading.CancellationToken.None), Times.Once);
        }