Beispiel #1
0
        [Test] // behaviour
        public void Update_When_entity_is_updated_Then_it_makes_calls_to_repository_and_unit_of_work()
        {
            // Arrange
            var airhostessDTOToUpdate = new AirhostessDTO()
            {
                Id        = 3,
                FirstName = "Airhostess2",
                LastName  = "Airhostess2",
                BirthDate = new DateTime(1970, 10, 1),
                CrewId    = 1
            };

            var airhostessRepositoryFake = A.Fake <IAirhostessRepository>();
            var unitOfWorkFake           = A.Fake <IUnitOfWork>();

            A.CallTo(() => unitOfWorkFake.Set <Airhostess>()).Returns(airhostessRepositoryFake);
            var airhostessService = new AirhostessService(unitOfWorkFake, AlwaysValidValidator);

            // Act
            var result = airhostessService.Update(airhostessDTOToUpdate);

            // Assert
            A.CallTo(() => airhostessRepositoryFake.Update(A <Airhostess> ._)).MustHaveHappenedOnceExactly();
            A.CallTo(() => unitOfWorkFake.Set <Airhostess>()).MustHaveHappenedOnceExactly();
            A.CallTo(() => unitOfWorkFake.SaveChanges()).MustHaveHappenedOnceExactly();
        }
Beispiel #2
0
        public void Update_When_entity_is_updated_Then_existing_entity_in_db_updates()
        {
            // Arrange
            var entityToUpdate = new AirhostessDTO
            {
                Id        = 1,
                FirstName = "Airhostess1",
                LastName  = "Airhostess1",
                BirthDate = new DateTime(1970, 1, 12),
                CrewId    = 1
            };

            var service = new AirhostessService(UnitOfWork, AlwaysValidValidator);

            // Act
            var updatedEntity = service.Update(entityToUpdate);

            // Assert
            var fromDbEntity = AirportDbContext.Airhostess.FirstOrDefault(x => x.Id == entityToUpdate.Id);

            Assert.NotNull(fromDbEntity);
            Assert.AreEqual(entityToUpdate.Id, fromDbEntity.Id);
            Assert.AreEqual(entityToUpdate.FirstName, fromDbEntity.FirstName);
            Assert.AreEqual(entityToUpdate.LastName, fromDbEntity.LastName);
            Assert.AreEqual(entityToUpdate.BirthDate, fromDbEntity.BirthDate);
            Assert.AreEqual(entityToUpdate.CrewId, fromDbEntity.CrewId);
        }
Beispiel #3
0
        public void Update_When_entity_is_updated_Then_updated_entity_is_returned()
        {
            // Arrange
            var airhostessMock = new Airhostess()
            {
                Id        = 3,
                FirstName = "Airhostess3",
                LastName  = "Airhostess3",
                BirthDate = new DateTime(1970, 10, 1),
                CrewId    = 3,
                Crew      = null
            };

            var airhostessDTOToUpdate = new AirhostessDTO()
            {
                Id        = 3,
                FirstName = "Airhostess2",
                LastName  = "Airhostess2",
                BirthDate = new DateTime(1970, 10, 1),
                CrewId    = 1
            };

            var expectedAirhostessDTO = new AirhostessDTO()
            {
                Id        = 3,
                FirstName = "Airhostess3",
                LastName  = "Airhostess3",
                BirthDate = new DateTime(1970, 10, 1),
                CrewId    = 3
            };
            var airhostessRepositoryFake = A.Fake <IAirhostessRepository>();

            A.CallTo(() => airhostessRepositoryFake.Update(A <Airhostess> ._)).Returns(airhostessMock);

            var unitOfWorkFake = A.Fake <IUnitOfWork>();

            A.CallTo(() => unitOfWorkFake.Set <Airhostess>()).Returns(airhostessRepositoryFake);

            var airhostessService = new AirhostessService(unitOfWorkFake, AlwaysValidValidator);

            // Act
            var result = airhostessService.Update(airhostessDTOToUpdate);

            // Assert
            Assert.AreEqual(expectedAirhostessDTO.Id, result.Id, "Id");
            Assert.AreEqual(expectedAirhostessDTO.FirstName, result.FirstName);
            Assert.AreEqual(expectedAirhostessDTO.LastName, result.LastName);
            Assert.AreEqual(expectedAirhostessDTO.BirthDate, result.BirthDate);
            Assert.AreEqual(expectedAirhostessDTO.CrewId, result.CrewId);
        }
Beispiel #4
0
        public void Update_When_entity_is_invalid_Then_bad_request_exception_is_thrown()
        {
            // Arrange
            var airhostessDTOToUpdate = new AirhostessDTO()
            {
                Id        = 3,
                FirstName = "Airhostess2",
                LastName  = "Airhostess2",
                BirthDate = new DateTime(1970, 10, 1),
                CrewId    = 2
            };

            var airhostessRepositoryFake = A.Fake <IAirhostessRepository>();
            var unitOfWorkFake           = A.Fake <IUnitOfWork>();
            var airhostessService        = new AirhostessService(unitOfWorkFake, AlwaysInValidValidator);

            // Act + Assert
            var exception = Assert.Throws <BadRequestException>(() => airhostessService.Update(airhostessDTOToUpdate), "");

            Assert.AreEqual(exception.Message, "Is Invalid");
        }
Beispiel #5
0
        public void Update_When_entity_is_updated_Then_other_entities_are_not_changed()
        {
            // Arrange
            var entityToUpdate = new AirhostessDTO
            {
                Id        = 1,
                FirstName = "Airhostess1",
                LastName  = "Airhostess1",
                BirthDate = new DateTime(1970, 1, 12),
                CrewId    = 1
            };

            var airhostessesBeforeUpdate = AirportDbContext.Airhostess
                                           .Where(x => x.Id != entityToUpdate.Id).ToList();

            var service = new AirhostessService(UnitOfWork, AlwaysValidValidator);

            // Act
            var updatedEntity = service.Update(entityToUpdate);

            var airhostessesAfterUpdate = AirportDbContext.Airhostess
                                          .Where(x => x.Id != entityToUpdate.Id).ToList();

            // Assert
            Assert.AreEqual(airhostessesBeforeUpdate.Count, airhostessesAfterUpdate.Count);
            foreach (var beforeUpdate in airhostessesBeforeUpdate)
            {
                var afterUpdate = airhostessesAfterUpdate.FirstOrDefault(x => x.Id == beforeUpdate.Id);
                Assert.NotNull(afterUpdate);

                Assert.AreEqual(beforeUpdate.Id, afterUpdate.Id);
                Assert.AreEqual(beforeUpdate.FirstName, afterUpdate.FirstName);
                Assert.AreEqual(beforeUpdate.LastName, afterUpdate.LastName);
                Assert.AreEqual(beforeUpdate.BirthDate, afterUpdate.BirthDate);
                Assert.AreEqual(beforeUpdate.CrewId, afterUpdate.CrewId);
            }
        }
Beispiel #6
0
        [Test] //behavior test
        public void Update_When_entity_is_invalid_Then_it_makes_no_calls_to_repository_and_unit_of_work()
        {
            // Arrange
            var airhostessDTOToUpdate = new AirhostessDTO()
            {
                Id        = 3,
                FirstName = "Airhostess2",
                LastName  = "Airhostess2",
                BirthDate = new DateTime(1970, 10, 1),
                CrewId    = 2
            };

            var airhostessRepositoryFake = A.Fake <IAirhostessRepository>();
            var unitOfWorkFake           = A.Fake <IUnitOfWork>();
            var airhostessService        = new AirhostessService(unitOfWorkFake, AlwaysInValidValidator);

            // Act + Assert
            var exception = Assert.Throws <BadRequestException>(() => airhostessService.Update(airhostessDTOToUpdate));

            A.CallTo(() => airhostessRepositoryFake.Update(A <Airhostess> ._)).MustNotHaveHappened();
            A.CallTo(() => unitOfWorkFake.AirhostessRepository).MustNotHaveHappened();
            A.CallTo(() => unitOfWorkFake.Set <Airhostess>()).MustNotHaveHappened();
            A.CallTo(() => unitOfWorkFake.SaveChanges()).MustNotHaveHappened();
        }