public async Task GetTodoItems_ReturnsHttpNotFound_ForInvalidTodoItem() { // Arrange int todoId = 111; var mockRepo = new Mock <ITodoItemRepository>(); mockRepo.Setup(repo => repo.GetAsync(todoId)) .ReturnsAsync((TodoItem)null); var controller = new TemporaryController(mockRepo.Object); // Act var result = await controller.Get(todoId); // Assert var notFoundObjectResult = Assert.IsType <NotFoundObjectResult>(result); Assert.Equal(todoId, notFoundObjectResult.Value); }
public async Task GetTodoItems_ReturnsTodoItems() { // Arrange int todoId = 1; var mockRepo = new Mock <ITodoItemRepository>(); mockRepo.Setup(repo => repo.GetAsync(todoId)) .ReturnsAsync(FakeDatasHelper.GetTestTodos().FirstOrDefault(t => t.ID == todoId)); var controller = new TemporaryController(mockRepo.Object); // Act var result = await controller.Get(todoId); // Assert var okResult = Assert.IsType <OkObjectResult>(result); var returnValue = Assert.IsType <TodoItem>(okResult.Value); Assert.False(returnValue.IsComplete); Assert.Equal("测试1", returnValue.Name); Assert.Equal(DateTimeOffset.Now.Day, returnValue.ModifyTime.Day); Assert.Equal(todoId, returnValue.ID); Assert.Equal(2, returnValue.Steps.Count); }