In our previous post, we used AutoMapper to convert from a entity to a view model. In our example code, it only works as expected in happy path mode where the configuration is valid and there is no way to know the configuration is not valid until we run the application.
In this post, we are going to look at how with just a few lines of code, we can write a unit test to validate our AutoMapper mapping profile.
In our working AutoMapper example in the previous post, if we change the WeatherForecastViewModel Summary property name to SummaryText and then run the xunit tests that are going to create in a moment, we will get the following error that lets us know that we have properties that are not mapped between the two objects.
If you changed the WeatherForecastViewModel Summary property name, go ahead and change it back to Summary.
Now we are going to create our XUnit project and mapping profile test.
In our project directory, create a new folder called AutoMapperExample.UnitTests
cd into AutoMapperExample.UnitTests
Create our XUnit project using the dotnet cli
dotnet new xunit
Add a reference to the AutoMapperExample.API project
dotnet add reference ..\AutoMapperExample.API\AutoMapperExample.csproj
Open the project in your code editor
Delete the file UnitTest1.cs
Create a new file named MappingProfileTests.cs
Add the following code to the MappingProfileTests.cs to load our mapping profile and assets that the configuration is valid
using AutoMapper; using Xunit; namespace AutoMapperExample.UnitTests; public class MappingProfileTests { [Fact] public void ValidateMappingConfigurationTest() { MapperConfiguration mapperConfig = new MapperConfiguration( cfg => { cfg.AddProfile(new MappingProfile()); }); IMapper mapper = new Mapper(mapperConfig); mapper.ConfigurationProvider.AssertConfigurationIsValid(); } }
Now you are able to validate that the AutoMapper mapping profile is valid as part of your testing and automated builds.
In our next post on AutoMapper, we will take a look at what to do when property names are not the same between the input and output.