Unit Testing Your Mappings
While AutoMappic provides build-time safety through Diagnostics AM0001-AM0017, it is still best practice to verify your most critical business mappings through automated tests.
Testing Your Profiles
You can test a mapping profile in isolation by creating a standalone Mapper instance in your test project:
[Fact]
public void UserProfile_ShouldMapCorrectly()
{
// 1. Setup the mapper with the specific profile
var config = new MapperConfiguration(cfg => cfg.AddProfile<UserProfile>());
var mapper = config.CreateMapper();
// 2. Arrange
var source = new User { Id = 1, Name = "Digvijay" };
// 3. Act
var dest = mapper.Map<User, UserDto>(source);
// 4. Assert
Assert.Equal("Digvijay", dest.Name);
}Verifying Configurations
Because AutoMappic is static, it doesn't need a runtime "AssertConfigurationIsValid" method like some other mappers. If your project builds, the configuration is valid.
However, you can still verify that expected interceptors have been generated by looking at the .g.cs files in the obj/ folder of your project.
Testing Asynchronous Mappings
When testing MapAsync, remember to await the call and ensure your test framework supports async tests:
[Fact]
public async Task MapAsync_WithRemoteService_Works()
{
var dest = await mapper.MapAsync<User, UserDto>(source);
Assert.NotNull(dest.AuditLog);
}Negative Testing (Advanced)
If you are building a library on top of AutoMappic, you may want to verify that the generator emits diagnostics for invalid mappings. To do this, we recommend using the Microsoft.CodeAnalysis.CSharp.Analyzer.Testing package.
See the AutoMappic Source Code for examples of how we test our own diagnostics!