AutoTest of all mappings in AutoMapperProfiles C#

Viewed 33

The question is to make a test that will look at each mapping from the MapperProfile and do a mapping test through .Map(model)

For Example:

private static IMapper _mapper;
public MappingAll()
{
    var mappingConfig = new MapperConfiguration(mc =>
    {
        mc.AddProfile(new Product());
        mc.AddProfile(new Apple());
    });
    var mapper = mappingConfig.CreateMapper();
    _mapper = mapper;
}

[Test]
public void TestAll()
{
    MapperProvider tt = new MapperProvider(_mapper);
    // call some method to check all mappings specified in mappingConfig
    tt.CheckAllMappings();
}

I tried to implement it through reflection, but it didn't work fine. There may also be a situation where mapping from an interface to a class can take place

1 Answers

You can verify the mappings like this:

var sut = new MapperConfiguration(cfg => cfg.AddProfile<YourProfile>());
sut.AssertConfigurationIsValid();

You can generate test data with something like AutoFixture, but how would you work out that test data has been correctly moved across to the destination?

Related