Base solution for your next web application
Open Closed

Unit testing app services and automapper #2954


User avatar
0
liam1983 created

Hi,

I try to make a unit test for one of my Services, the problem is my custom mapping didnt happen. What did I wrong? I would like to map special Sub entities to only a string.

[AutoMap(typeof(Coin))]
    public class CoinListDto : FullAuditedEntityDto<Guid>
    {
        [Range(0, Int32.MaxValue)]
        public int? Amount { get; set; }

        [Required(AllowEmptyStrings = false)]
        [MaxLength(500)]
        public string Name { get; set; }

        [Range(-2000, 9999)]
        public int Year { get; set; }

        [Range(0, Int32.MaxValue)]
        public int NominalValue { get; set; }

        public string Country { get; set; }

        public string Currency { get; set; }
    }

in my module

public override void PostInitialize()
        {
            base.PostInitialize();
            Configuration.Modules.AbpAutoMapper().Configurators.Add(
                mapper =>
                    {
                        mapper.CreateMap<Entities.Coin.Coin, CoinListDto>();
                        mapper.CreateMap<Entities.Coin.Coin, CoinListDto>()
                            .ForMember(dto => dto.Country, opt => opt.MapFrom(coin => coin.Country.Name));
                        mapper.CreateMap<Entities.Coin.Coin, CoinListDto>()
                            .ForMember(dto => dto.Currency, opt => opt.MapFrom(coin => coin.Currency.Name));
                    });
        }

but when I execute the test my custom mappings dont happen


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you move it to PreInitialize. Also what happens when you put a breakpoint at this line ? Does it hit ?

    Configuration.Modules.AbpAutoMapper().Configurators.Add(
                    mapper =>
                        {
                            mapper.CreateMap<Entities.Coin.Coin, CoinListDto>();
                            mapper.CreateMap<Entities.Coin.Coin, CoinListDto>()
                                .ForMember(dto => dto.Country, opt => opt.MapFrom(coin => coin.Country.Name));
                            mapper.CreateMap<Entities.Coin.Coin, CoinListDto>()
                                .ForMember(dto => dto.Currency, opt => opt.MapFrom(coin => coin.Currency.Name));
                        });
    

    Thanks.