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();
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...
I use Refit to call web api in my code I also like use StatusCode 422 when the web api have to report that there is a logic error in the application and not an exception and use test, to verify the code. So I want write here about this problem, how, with mock and Refit we can test that the application report correctly the 422 status code and a string error(or anything else) WebApi My web api have a class with a response and an error property. public class Response { public string Error { get; set; } public void SetError(string error) { Error = error; } } And the interface of refit public interface IClient { [Post("/Controller/MyMethod")] Task MyMethod([Body]Parameters parameters); } The working code could be something like this public string ApiCaller(IClient client) { try { await client.MyMethod ( new Parameters(...); ); } catch(ApiException ex) when (ex.StatusCode == HttpStatus...
Comments
Post a Comment