Created
March 24, 2011 20:24
-
-
Save jeffdeville/885805 to your computer and use it in GitHub Desktop.
Automapper configuration in a way that is defensive, but still tight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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