Search code examples
.netunit-testing.net-6.0mappermapster

Mapper Mapster - How to unit test config?


I come here because i dont find the answer online :/

I would like to test my configuration of Mapster to avoid miss mapping which happens at developpement time. But i dont find how to implement that with Mapster lib.

Any clue ?

Regards,


Solution

  • You can unit test your mapster configuration by created a new mapper instance and adding your implementation classes of the IRegister to the config.

    This code will search for IRegister interfaces and inject them into a instance of the mapster mapper.

    public static class AddMapsterForUnitTests
    {
        public static Mapper GetMapper()
        {
            var config = TypeAdapterConfig.GlobalSettings;
            config.Scan(typeof(ImplementationOfIRegisterClass).Assembly);
            return new Mapper(config);
        }
    }
    

    And then in your unit test Arrange method:

    // Arrange
    var mapper = AddMapsterForUnitTests.GetMapper();