Tuesday, January 29, 2019

Write a unit test which will crawl all of your AutoMapper mappings and make sure nothing is missing!

When you add a new column to a database table and, along those lines, a new getsetter at a corresponding POCO, well, you'd better not leave it out of any of your AutoMapper mappings that use that type or else this test will break, well, unless you have the .ForAllOtherMembers(opt => opt.Ignore()) trick the mappings that might otherwise squawk.

using Smorgasbord.Features.Common;
using Xunit;
namespace Smorgasbord.Tests.Features.Common
{
   public class AutoMapperProfileConfigurationTests
   {
      [Fact]
      public void AutoMapperConfiguration_IsValid()
      {
         var config = new AutoMapper.MapperConfiguration(cfg =>
         {
            cfg.AddProfile(new SmorgasbordConfiguration());
         });
         var mapper = config.CreateMapper();
         mapper.ConfigurationProvider.AssertConfigurationIsValid();
      }
   }
}

 
 

SmorgasbordConfiguration here would inheirt from AutoMapper.Profile and live in the Smorgasbord.Features.Common namespace. Best unit test ever?

No comments:

Post a Comment