Skip to content

Instantly share code, notes, and snippets.

@jeffdeville
Created March 24, 2011 20:24
Show Gist options
  • Select an option

  • Save jeffdeville/885805 to your computer and use it in GitHub Desktop.

Select an option

Save jeffdeville/885805 to your computer and use it in GitHub Desktop.
Automapper configuration in a way that is defensive, but still tight
There are several advantages here to testing the negative case, rather than the positive
1. You're not testing what we can trust Automapper to do
2. You _are_ testing the customizations to Automapper specific to your scenario
3. Your tests will be much shorter
4. Your tests will be less brittle (if you add a field to one your source, but forget the destination, you probably also forgot to update your mapper specs too, which means nothing will be caught. This test strategy will catch that problem)
(Example above in MSpec)
It should_map_all_fields = () =>
{
GetUnmappedPropertiesFor<AdvertisementEditModel, Advertisement>()
.ShouldContainOnly(new[] { "AdLayout" });
};
public static string[] GetUnmappedPropertiesFor<SRC,DEST>()
{
return AutoMapper.Mapper.FindTypeMapFor(typeof(SRC), typeof(DEST)).GetUnmappedPropertyNames();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment