public async Task Update() { //Arrange var surveyuser = new SurveyUserUpdate { Id = 1, FirstName = "First Name", LastName = "Last name", DoB = DateTime.Today.AddYears(-21), Gender = Data.Enums.Gender.Male, Country = "Makedonija" }; //Act var result = await _service.Update(surveyuser); //Assert result.Should().NotBeNull(); result.Should().BeAssignableTo <SurveyUserBaseModel>(); result.Id.Should().Be(surveyuser.Id); result.FirstName.Should().Be(surveyuser.FirstName); result.LastName.Should().Be(surveyuser.LastName); result.Country.Should().Be(surveyuser.Country); result.Gender.Should().Be(surveyuser.Gender); }
public async Task <IActionResult> Put(int id, [FromBody] SurveyUserUpdate model) { if (ModelState.IsValid) { model.Id = id; var result = await _service.Update(model); return(result != null ? (IActionResult)Ok(result) : NoContent()); } return(BadRequest()); }
public async Task <SurveyUserBaseModel> Update(SurveyUserUpdate model) { var entity = await _context.SurveyUsers.FindAsync(model.Id); if (entity == null) { throw new Exception("User not found"); } _mapper.Map(model, entity); _context.SurveyUsers.Attach(entity); _context.Entry(entity).State = EntityState.Modified; await SaveAsync(); return(_mapper.Map <SurveyUserBaseModel>(entity)); }
public async Task ThrowExceptionOnUpdateMatch() { //Arrange var surveyUser = new SurveyUserUpdate { Id = 20, FirstName = "First Name", LastName = "Last name", DoB = DateTime.Today.AddYears(-21), Gender = Data.Enums.Gender.Male, Country = "Makedonija" }; //Act & Assert var ex = await Assert.ThrowsAsync <Exception>(() => _service.Update(surveyUser)); Assert.Equal("Match not found", ex.Message); }