public async Task IndexPost_ReturnsARedirectAndAddsTodo_WhenModelStateIsValid() { // Arrange var mockRepo = new Mock <ITodoItemRepository>(); mockRepo.Setup(repo => repo.AddAsync(It.IsAny <TodoItem>())) .Returns(Task.CompletedTask) .Verifiable(); var controller = new TemporaryController(mockRepo.Object); var newTodo = new TemporaryController.NewTodoModel() { Name = "测试" }; // Act var result = await controller.Index(newTodo); // Assert var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); Assert.Null(redirectToActionResult.ControllerName); Assert.Equal("Index", redirectToActionResult.ActionName); mockRepo.Verify(); }
public async Task Index_ReturnsAViewResult_WithAListOfTodoItems() { // Arrange var mockRepo = new Mock <ITodoItemRepository>(); mockRepo.Setup(repo => repo.ListAsync()) .ReturnsAsync(FakeDatasHelper.GetTestTodos()); var controller = new TemporaryController(mockRepo.Object); // Act var result = await controller.Index(); // Assert var viewResult = Assert.IsType <ViewResult>(result); var model = Assert.IsAssignableFrom <IEnumerable <TodoItemViewModel> >(viewResult.ViewData.Model); Assert.Equal(2, model.Count()); }
public async Task IndexPost_ReturnsARedirectAndAddsTodo_WhenModelStateIsInValid() { // Arrange var mockRepo = new Mock <ITodoItemRepository>(); mockRepo.Setup(repo => repo.ListAsync()) .ReturnsAsync(FakeDatasHelper.GetTestTodos()); var controller = new TemporaryController(mockRepo.Object); controller.ModelState.AddModelError("Name", "Required"); var newTodo = new TemporaryController.NewTodoModel(); // Act var result = await controller.Index(newTodo); // Assert var badReqeustResult = Assert.IsType <BadRequestObjectResult>(result); Assert.IsType <SerializableError>(badReqeustResult.Value); }