public async Task <IActionResult> Create([Bind("SomeNumber, SomeName")] CompositRepoViewModel model) { if (ModelState.IsValid) { if (model.SomeName == model.SomeNumber.ToString()) { if (int.TryParse(model.SomeNumber, out int someNumber)) { _unitOfWork.Repo1.Add(new Repo1Item() { SomeNumber = someNumber }); _unitOfWork.Repo2.Add(new Repo2Item() { SomeName = model.SomeName }); await _unitOfWork.CompleteAsync(); return(RedirectToAction(nameof(Create))); } ModelState.AddModelError("number_not_an_int", "SomeNumber is not a number"); } else { ModelState.AddModelError("name_and_value_not_equal", "SomeName and SomeNumber are not equal"); } } model.Repo1Items = _unitOfWork.Repo1.GetAllItems(); model.Repo2Items = _unitOfWork.Repo2.GetAllItems(); return(View(model)); }
// GET: Repo1Item public IActionResult Create() { var model = new CompositRepoViewModel(); model.Repo1Items = _unitOfWork.Repo1.GetAllItems(); model.Repo2Items = _unitOfWork.Repo2.GetAllItems(); return(View(model)); }
public void Create_ValuesAreNonNumeric_ShouldReturnModelStateInvalid() { // Arrange var model = new CompositRepoViewModel(); model.SomeName = "foo"; model.SomeNumber = "foo"; // Act var task = _compositeRepoController.Create(model); if (task.Result is ViewResult viewResult) { // Assert var modelState = viewResult.ViewData.ModelState; modelState.IsValid.Should().BeFalse(); //Assert.IsFalse(modelState.IsValid, "Create with values foo Should give ModelState Invalid"); return; } Assert.Fail("Unexpected return type"); }
public void Create_ValuesAreNotEqual_ShouldReturnModelStateInvalid() { // Arrange var model = new CompositRepoViewModel(); model.SomeName = "foo"; model.SomeNumber = "123"; // Act var task = _compositeRepoController.Create(model); if (task.Result is ViewResult viewResult) { var modelState = viewResult.ViewData.ModelState; // Assert Assert.IsFalse(modelState.IsValid, "Submit someName foo and SomeNumber 123 Should give ModelState Invalid"); return; } Assert.Fail("Unexpected return type"); }
public void Create_ValuesAreNumeric_ShouldReturnRedirectActionNameCreate() { // Arrange var model = new CompositRepoViewModel(); model.SomeName = "123"; model.SomeNumber = "123"; // Act var task = _compositeRepoController.Create(model); if (task.Result is RedirectToActionResult redirect) { var actualActionName = redirect.ActionName; var expectedActionName = "Create"; // Assert Assert.AreSame(expectedActionName, actualActionName, "Create with equal numbers Should return RedirectToActionResult and ActionName Create"); return; } Assert.Fail("Unexpected return type"); }