public async Task <IActionResult> Index(ItemToDoModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } await _itemToDoRepository.AddAsync(new ItemTodo { Id = Guid.NewGuid(), Name = model.Name }); return(RedirectToAction(actionName: nameof(Index))); }
public async Task ShouldHavePassWhenModelStateIsValidAndReturnsARedirectAsync() { //arrange _itemToDoRepository.Setup(repo => repo.BrowsAsync()) .ReturnsAsync(_items); var controller = new HomeController(_itemToDoRepository.Object); var command = new ItemToDoModel { Name = "Hello world!" }; //actual var result = await controller.Index(command); //assert Assert.IsInstanceOf <RedirectToActionResult>(result); var action = result as RedirectToActionResult; Assert.AreEqual("Index", action.ActionName); }
public async Task ShouldHaveNotPassWhenPostIndexIsInValidModelStateReturnBadRequestResultAsync() { //arrange _itemToDoRepository.Setup(repo => repo.BrowsAsync()) .ReturnsAsync(_items); var controller = new HomeController(_itemToDoRepository.Object); controller.ModelState.AddModelError("model.Name", "error happened"); var command = new ItemToDoModel(); //actual var result = await controller.Index(command); //assert Assert.IsInstanceOf <BadRequestObjectResult>(result); var badRequest = result as BadRequestObjectResult; Assert.IsInstanceOf <SerializableError>(badRequest.Value); }