Beispiel #1
0
        public void Map_HasExcludedProperties_ReturnsUpdatedObject()
        {
            // Arrange
            Mapper <Animal, AnimalDTO> mapper = new Mapper <Animal, AnimalDTO>();
            AnimalDTO expectedRattlesnakeDTO  = new RattlesnakeDTO()
            {
                Name        = "Name",
                Length      = 10.0,
                IsPoisonous = true,
                Skill       = SpecialSkill.BITE
            };
            Animal    rattlesnake = new Rattlesnake("Name", 10.0, true, SpecialSkill.BITE);
            AnimalDTO actualRattlesnakeDTO;

            // Act
            actualRattlesnakeDTO   = (RattlesnakeDTO)mapper.Map(rattlesnake, expectedRattlesnakeDTO);
            expectedRattlesnakeDTO = new RattlesnakeDTO()
            {
                Name        = "Name",
                Length      = 10.0,
                IsPoisonous = true,
                Skill       = SpecialSkill.BITE
            };

            // Assert
            foreach (PropertyInfo property in actualRattlesnakeDTO.GetType().GetProperties())
            {
                object expectedValue = expectedRattlesnakeDTO
                                       .GetType()
                                       .GetProperty(property.Name)
                                       .GetValue(expectedRattlesnakeDTO);

                object actualValue = property
                                     .GetValue(actualRattlesnakeDTO);

                Assert.AreEqual(expectedValue, actualValue, $"{property.Name} is not as expected");
            }
        }
        public void ConvertCollection_CollectionOfEntities_ReturnsCollectionOfDTOs()
        {
            // Arrange
            Producer <Animal, AnimalDTO> producer = new Producer <Animal, AnimalDTO>();
            IList <Animal>    animals             = animalsBase;
            IList <AnimalDTO> expectedAnimalDTOs  = animalDTOsBase;
            IList <AnimalDTO> animalDTOs;

            // Act
            animalDTOs = producer.ConvertCollection(animals);

            // Assert
            foreach (AnimalDTO animalDTO in animalDTOs)
            {
                int       index    = animalDTOs.IndexOf(animalDTO);
                AnimalDTO expected = expectedAnimalDTOs[index];

                switch (animalDTO)
                {
                case PigeonDTO _:
                {
                    PigeonDTO pigeon         = animalDTO as PigeonDTO;
                    PigeonDTO expectedPigeon = expected as PigeonDTO;
                    foreach (PropertyInfo property in pigeon.GetType().GetProperties())
                    {
                        Assert.AreEqual(
                            expectedPigeon.GetType().GetProperty(property.Name),
                            pigeon.GetType().GetProperty(property.Name),
                            $"{property.Name} does not match.");
                    }
                }
                break;

                case HawkDTO _:
                {
                    HawkDTO hawk         = animalDTO as HawkDTO;
                    HawkDTO expectedHawk = expected as HawkDTO;
                    foreach (PropertyInfo property in hawk.GetType().GetProperties())
                    {
                        Assert.AreEqual(
                            expectedHawk.GetType().GetProperty(property.Name),
                            hawk.GetType().GetProperty(property.Name),
                            $"{property.Name} does not match.");
                    }
                }
                break;

                case RattlesnakeDTO _:
                {
                    RattlesnakeDTO rattlesnake         = animalDTO as RattlesnakeDTO;
                    RattlesnakeDTO expectedRattleSnake = expected as RattlesnakeDTO;
                    foreach (PropertyInfo property in rattlesnake.GetType().GetProperties())
                    {
                        Assert.AreEqual(
                            expectedRattleSnake.GetType().GetProperty(property.Name),
                            rattlesnake.GetType().GetProperty(property.Name),
                            $"{property.Name} does not match.");
                    }
                }
                break;
                }
            }
        }