예제 #1
0
        public async Task PilotControllerPutUpdatesPilot()
        {
            var initialPilot = (await _pilotsController.Get(1) as OkObjectResult)?.Value as PilotDto;

            Assert.IsNotNull(initialPilot);

            var updateSecondName = "TesterUpdated";
            var updateFirstName  = "PilotUpdated";
            var updateBirthDate  = new DateTime(2018, 12, 12);
            var updateExperience = 1;

            var pilotUpdateRequest = new EditablePilotFields
            {
                SecondName = updateSecondName,
                FirstName  = updateFirstName,
                BirthDate  = updateBirthDate,
                Experience = updateExperience
            };

            var updateResult = await _pilotsController.Put(1, pilotUpdateRequest) as OkResult;

            Assert.IsNotNull(updateResult);
            Assert.AreEqual(200, updateResult.StatusCode);

            var updatedPilot = (await _pilotsController.Get(1) as OkObjectResult)?.Value as PilotDto;

            Assert.IsNotNull(updatedPilot);

            Assert.AreEqual(updateSecondName, updatedPilot.SecondName);
            Assert.AreEqual(updateFirstName, updatedPilot.FirstName);
            Assert.AreEqual(updateBirthDate, updatedPilot.BirthDate);
            Assert.AreEqual(updateExperience, updatedPilot.Experience);

            Assert.AreEqual(initialPilot.Id, updatedPilot.Id);
            Assert.AreNotEqual(initialPilot.FirstName, updatedPilot.FirstName);
            Assert.AreNotEqual(initialPilot.SecondName, updatedPilot.SecondName);
            Assert.AreNotEqual(initialPilot.BirthDate, updatedPilot.BirthDate);
            Assert.AreNotEqual(initialPilot.Experience, updatedPilot.Experience);
        }
        public void Put_When_id_is_not_exist_Then_return_status_code_404()
        {
            var service    = A.Fake <IService <PilotDto> >();
            var notExistId = 8485;

            A.CallTo(() => service.Update(notExistId, A <PilotDto> ._)).Throws(new NotExistException());
            var controller = new PilotsController(service);
            var dto        = new PilotDto();
            var id         = notExistId;

            var result = controller.Put(id, dto) as StatusCodeResult;

            Assert.AreEqual(404, result.StatusCode);
        }
        public void Put_When_dto_is_valid_and_id_is_exist_Then_return_status_code_200()
        {
            var service  = A.Fake <IService <PilotDto> >();
            var validDto = new PilotDto
            {
                FirstName  = "Sanya",
                LastName   = "Alexanrovich",
                Birthdate  = new DateTime(1989, 12, 19),
                Experience = 10
            };
            var existId = 1;

            A.CallTo(() => service.Update(existId, validDto));
            var controller = new PilotsController(service);
            var dto        = validDto;
            var id         = existId;

            var result = controller.Put(id, dto) as StatusCodeResult;

            Assert.AreEqual(200, result.StatusCode);
        }
예제 #4
0
        public void Put_ModelIsIncorrect_Returns_StatusCode400()
        {
            int Id    = 1;
            var pilot = new PilotDTO {
                Id = 1, FirstName = "", LastName = "Sidorov", Experience = 255
            };
            var ServiceMock = new Mock <IService <PilotDTO> >();

            //when model is incorrect this method will throw ArgumentNullException
            ServiceMock.Setup(s => s.UpdateAsync(Id, pilot)).Throws(new Exception());

            PilotsController controller = new PilotsController(ServiceMock.Object);

            var context = new ValidationContext(pilot, null, null);
            var result  = new List <ValidationResult>();

            var valid = Validator.TryValidateObject(pilot, context, result, false);

            var message = controller.Put(Id, pilot).Result;


            Assert.False(valid);
            Assert.Equal(HttpStatusCode.BadRequest, message.StatusCode);
        }