Beispiel #1
0
            public void should_replace_target_collections()
            {
                var source = new SourceProduct
                {
                    Id       = 10,
                    Variants = new List <SourceProductVariant>
                    {
                        new SourceProductVariant {
                            Id = 1, Name = "variant 1"
                        },
                        new SourceProductVariant {
                            Id = 2, Name = "variant 2"
                        }
                    }
                };

                var mapper  = new DefaultObjectMapper();
                var context = new MappingContext();

                context.Includes.Add("Variants");

                var target = new TargetProduct();

                target.Variants = new List <TargetProductVariant>();
                target.Variants.Add(new TargetProductVariant {
                    Id = 3, Name = "variant 3"
                });

                target = mapper.Map(source, target, typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal(2, target.Variants.Count);
                Assert.Equal(1, target.Variants.First().Id);
                Assert.Equal("variant 1", target.Variants.First().Name);
                Assert.Equal(2, target.Variants.Skip(1).First().Id);
                Assert.Equal("variant 2", target.Variants.Skip(1).First().Name);
            }
            public void should_replace_target_collections()
            {
                var source = new SourceProduct
                {
                    Id = 10,
                    Variants = new List<SourceProductVariant>
                    {
                        new SourceProductVariant { Id = 1, Name = "variant 1"},
                        new SourceProductVariant { Id = 2, Name = "variant 2"}
                    }
                };

                var mapper = new DefaultObjectMapper();
                var context = new MappingContext();
                context.Includes.Add("Variants");

                var target = new TargetProduct();
                target.Variants = new List<TargetProductVariant>();
                target.Variants.Add(new TargetProductVariant { Id = 3, Name = "variant 3" });

                target = mapper.Map(source, target, typeof(SourceProduct), typeof(TargetProduct), null, context) as TargetProduct;

                Assert.Equal(2, target.Variants.Count);
                Assert.Equal(1, target.Variants.First().Id);
                Assert.Equal("variant 1", target.Variants.First().Name);
                Assert.Equal(2, target.Variants.Skip(1).First().Id);
                Assert.Equal("variant 2", target.Variants.Skip(1).First().Name);
            }