public void Post_Create_Model_Is_Not_Valid() { // Arrange var componentViewModel = new ComponentViewModel() { Name = "Sałata 11" }; var repository = Substitute.For <IComponentRepository>(); var mapper = Substitute.For <IMapper>(); var controller = new ComponentController(repository, mapper); var validator = new ModelValidator <ComponentViewModel>(componentViewModel); // Act var valid = validator.IsValid(); validator.AddToModelError(controller); var result = controller.Create(componentViewModel) as RedirectToRouteResult; var actionName = result.RouteValues.Values.ElementAt(0); var tempData = controller.TempData["ModelIsNotValid"]; // Assert Assert.That(result, !Is.Null); Assert.That("Wystąpił błąd w formularzu, spróbuj ponownie.", Is.EqualTo(tempData)); Assert.That("Index", Is.EqualTo(actionName)); Assert.That(valid, Is.False); }
public void Post_Create_Model_Is_Valid() { // Arrange var component = new Component() { Name = "Sałata" }; var componentViewModel = new ComponentViewModel() { Name = "Sałata" }; var repository = Substitute.For <IComponentRepository>(); var mapper = Substitute.For <IMapper>(); var controller = new ComponentController(repository, mapper); var validator = new ModelValidator <ComponentViewModel>(componentViewModel); mapper.Map <ComponentViewModel, Component>(componentViewModel).Returns(component); repository.Insert(component); repository.Save(); // Act var valid = validator.IsValid(); var result = controller.Create(componentViewModel) as RedirectToRouteResult; var actionName = result.RouteValues.Values.ElementAt(0); //only action name // Assert Assert.That(result, !Is.Null); Assert.That("Index", Is.EqualTo(actionName)); Assert.That(valid, Is.True); }