Identifying All Bad Mappings with AutoMapper

One of the long-running annoyances we’ve had with our test of AutoMapper configuration validity on my current project is that a test failure only revealed the first mapping that was wrong. I haven’t figured out why this is the case, but I’ve come up with a work-around that displays all the necessary information.

Because the exception thrown if one or more incorrect mappings is found is AutoMapperConfigurationException, my revised test catches that exception in order to print the source type, destination type, and the list of unmapped property names. Re-throwing the exception at the end ensures that the test still reports a failure. The XUnit test which demonstrates this is available as a GitHub gist. If you’re using NUnit or MSTest in your application, minor revisions to this test will give you the same results.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.