Validate Automapper configurations with AssertConfigurationIsValid
Get link
Facebook
X
Pinterest
Email
Other Apps
Automapper permit to map a class on another in order to transform source object into a destination object.
We can do this transformation declaring mappings into a file for example MyProfile1.
We could have complex transformation for each property of the object or we can use an automapper behaviour that permit to copy the contents of property between the objects if thay have the same name property.
Just with this possibility that allows us to save lines of codes, we could find a problem.
If we rename one of the property the copy will be lost, and we'll find this problem at runtime.
To overcome this problem, we can use the verification feature of AutoMapper, AssertConfigurationIsValid. We put this line of code in program.cs, at the start of the application to check mappings.
var mapper = app.Services.GetService();
mapper.ConfigurationProvider.AssertConfigurationIsValid();
For example if we have this two classes
public class Source
{
public string Name { get; set; }
public string Surname { get; set; }
public string DescriptionRole { get; set; }
}
public class Destination
{
public string Name { get; set; }
public string Surname { get; set; }
public string Role { get; set; }
}
We renamed into the class Destination the property DescriptionRole in Role. If your mapping is
public class MyProfile1
{
public MyProfile1()
{
CreateMap()
.ForMember(dest => dest.Surname, opt
=> opt.MapFrom(src => src.Surname))
.ForMember(dest => dest.Name, opt
=> opt.MapFrom(src => src.Name));
}
}
The error when you start the application will be
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
==================================================================================================================================================================
Source -> Destination (Destination member list)
MyNameSpace.Source -> MyNameSpace.Destination(Destination member list)
Unmapped properties:
DescriptionRole
If you don't want to map this property, you can use Ignore.
Another problem occured when you define CreateMap multiple times in the same Profile class or another, the automapper use the last one loaded, but the could be different from each other.
With AssertConfigurationIsValid, if tou define the same trasformation in MyProfile1 and MyProfile2, the error will be
The following type maps were found in multiple profiles:
MyNamespace.Source to MyNamespace.Destination defined in profiles:
MyNamespace.MyProfile1
MyNamespace.MyProfile2
Sometimes we've to test a class that extend an anstract class and use other components of the project. For example, we have a ExcelLoader that extend an abstract class Loader, that read an excel file and use data inserted in the file to send mail or send SMS to some users. ExcelLoader It's an important class of our project so it's usefull cover this class with some test, but could be difficult test the class with really instance of the components that send mail and SMS. So we want test only the behavior of ExcelLoader The code public abstract class Loader { public abstract void ImportFile(Stream stream); public abstract IEnumerable<RowFile> GetRows(Stream stream); } public class ExcelLoader : Loader { IMailManager mailManager; ISMSManager smsManager; public ExcelLoader(IMailManager mailManager, ISMSManager smsManager) { this.mailManager = mailManager; this.smsManager = sm...
Writing test could be boring and stakeholders could know how your software should behave. SpecFlow could help you. It's a framework for BDD that uses an extension of Visual Studio that transforms the feature asked by user into code to test. SpecFlow allows you to write the behaviors in your native language and displays the data in a form like Excel table. Install SpecFlow You have to install the extension for Visual Studio in your test project. Install this package, nuget SpecFlow , SpecFlow.Tools.MsBuild.Generation , that will generate the code from our IDE and SpecFlow.xUnit ( if you're using xUnit) that will allow Visual Studio to find the test that specFlow will generate automatically. Background Suppose you want to create tests for a fruit and vegetable warehouse with online purchase. However, be careful to split the sections correctly and it is what I'd like to dwell on. Background is the section where you collect all tha...
Comments
Post a Comment