コード例 #1
0
        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);
        }
コード例 #2
0
        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());
        }
コード例 #3
0
        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));
        }
コード例 #4
0
        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);
        }