Unit test the Automapper profiles

Pascal picture Pascal · Jan 6, 2013 · Viewed 38.4k times · Source

I do want to test the custom logic in the CreateMap method. I do NOT want to test whether the mapping exist at all for some types.

How can I do that or what are the classes that I need to know. I am grateful for every hint The document about. Automapper unit testing seems very rare...

public class UnitProfile : Profile
{
   protected override void Configure()
   {
      // Here I create my maps with custom logic that needs to be tested

    CreateMap<Unit, UnitTreeViewModel>()
         .ForMember(dest => dest.IsFolder, o => o.MapFrom(src => src.UnitTypeState == UnitType.Folder ? true : false));

    CreateMap<CreateUnitViewModel, Unit>()
         .ForMember(dest => dest.UnitTypeState, o => o.MapFrom(src => (UnitType)Enum.ToObject(typeof(UnitType), src.SelectedFolderTypeId)));
   }
}

Answer

Mightymuke picture Mightymuke · Jan 6, 2013

This is the documentation for configuration testing: http://docs.automapper.org/en/stable/Configuration-validation.html

You can see an example of it here: https://stackoverflow.com/a/14150006/1505426