cezarypiatek / MappingGenerator

:arrows_counterclockwise: "AutoMapper" like, Roslyn based, code fix provider that allows to generate mapping code in design time.
https://marketplace.visualstudio.com/items?itemName=54748ff9-45fc-43c2-8ec5-cf7912bc3b84.mappinggenerator
MIT License
1.03k stars 120 forks source link

Generate unit tests #148

Closed WojciechNagorski closed 3 years ago

WojciechNagorski commented 3 years ago

I know that this issue is a little out of the scope of this project. I've seen https://github.com/cezarypiatek/MappingGenerator/issues/131.

I wanted to change AutoMapper to MappingGenerator but I have one blocker. I have many profiles in my program (the same situation is in many other projects in my company) and I have one unit test that checks the validation of mapping

           foreach (var profile in profiles)
            {
                var mapperConfig = new MapperConfiguration(cfg => { cfg.AddProfile(profile); });
                try
                {
                    mapperConfig.AssertConfigurationIsValid();
                }
                catch (AutoMapperConfigurationException ex)
                {
                    // I know that there is a problem
                }
            }

If someone adds a property to an object, the test will show him an error. I can't reproduce this behavior with MappingGenerator without creating a unit test for each mapping. But it requires a lot of work and the reviewer have to check if the test is added.

So if I have junior devs in my team I prefer to stay with AutoMapper with its disadvantages (I know it is slow, it is hard to debug, IDE doesn't show references of property) but I know that mappings are ok (or it is highly probable)

Any ideas on how to automate it?

cezarypiatek commented 3 years ago

I'm not familiar with the profiles (or I don't remember it, because it's been round 6 years since I used AutoMapper the last time). If you want to track unmapped fields then I would recommend trying to use /*FullInitRequired:recursive*/ marker with my other analyzer described here https://cezarypiatek.github.io/post/immutable-types-with-roslyn/ This should solve the problem at least from the target object perspective.

cezarypiatek commented 3 years ago

@WojciechNagorski does it answer your question?

WojciechNagorski commented 3 years ago

Yes, We can close this issue. But I'm not sure if this solution is enough. I need to try it.