public IActionResult Post(PersonAccomplishmentViewModel model) { if (_accomplishment.Create(model)) { return(Ok()); } return(BadRequest()); }
public void Create_InvalidModelState_CreatePersonAccomplishmentNeverExecutes() { _controller.ModelState.AddModelError("FirstName", "Name is required"); var personAccomplishment = new PersonAccomplishmentViewModel { LastName = "Test LastName", City = "CityTest", State = "Test state", DateOfBirth = new DateTime(1978, 4, 5) }; _controller.Create(personAccomplishment); _mockService.Verify(x => x.Create(It.IsAny <PersonAccomplishmentViewModel>()), Times.Never); }
public IActionResult Create([FromForm] PersonAccomplishmentViewModel model) { if (ModelState.IsValid) { if (_accomplishment.Create(model)) { return(RedirectToAction("List")); } } ModelState.AddModelError("", "Fail to create user"); return(View(model)); }
public void Create_InvalidModelState_ReturnsView() { _controller.ModelState.AddModelError("FirstName", "Name is required"); var personAccomplishment = new PersonAccomplishmentViewModel { LastName = "Test LastName", City = "CityTest", State = "Test state", DateOfBirth = new DateTime(1978, 4, 5) }; var result = _controller.Create(personAccomplishment); var viewResult = Assert.IsType <ViewResult>(result); var testPerson = Assert.IsType <PersonAccomplishmentViewModel>(viewResult.Model); Assert.Equal(personAccomplishment.LastName, testPerson.LastName); Assert.Equal(personAccomplishment.State, testPerson.State); }
public void Create_ActionExecuted_RedirectsToIndexAction() { var createPerson = new PersonAccomplishmentViewModel { FirstName = "Test FirstName", LastName = "Test LastName", City = "CityTest", State = "Test state", DateOfBirth = new DateTime(1978, 4, 5) }; _mockService.Setup(service => service.Create(createPerson)).Returns(true); var result = _controller.Create(createPerson); var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); Assert.Equal("List", redirectToActionResult.ActionName); }
public bool Create(PersonAccomplishmentViewModel model) { var person = new Person { FirstName = model.FirstName, LastName = model.LastName, Birthdate = model.DateOfBirth, City = model.City, State = model.State }; _context.People.Add(person); if (_context.SaveChanges() > -1) { return(true); } return(false); }
public void Create_ModelStateValid_CreatePersonAccomplishmentCalledOnce() { PersonAccomplishmentViewModel personAccomplishment = null; _mockService.Setup(r => r.Create(It.IsAny <PersonAccomplishmentViewModel>())) .Callback <PersonAccomplishmentViewModel>(x => personAccomplishment = x); var createPerson = new PersonAccomplishmentViewModel { FirstName = "Test FirstName", LastName = "Test LastName", City = "CityTest", State = "Test state", DateOfBirth = new DateTime(1978, 4, 5) }; _controller.Create(createPerson); _mockService.Verify(x => x.Create(It.IsAny <PersonAccomplishmentViewModel>()), Times.Once); Assert.Equal(personAccomplishment.FirstName, createPerson.FirstName); Assert.Equal(personAccomplishment.LastName, createPerson.LastName); Assert.Equal(personAccomplishment.State, createPerson.State); }
public void AccomplishmentService_Return_Accomplishment_Record_Created_ReturnTrue() { var model = new PersonAccomplishmentViewModel { PersonId = 8, FirstName = "NewfirstName", LastName = "NewLastName", DateOfBirth = new DateTime(1989, 11, 03), City = "NewCity", State = "NewState" }; var options = new DbContextOptionsBuilder <DataContext>() .UseInMemoryDatabase(databaseName: "recipedb") .Options; using (var context = new DataContext(options)) { var service = new AccomplishmentService(context); var result = service.Create(model); Assert.True(result); } }