How do I know if automapper has already been initialized?
In our particular case, we had multiple unit tests competing for the same instance of AutoMapper. Even if we used the Reset() method, it threw the error that it was already initialized. We solved the problem with a singleton instance that is inherited by each unit test class in our tests project.
public class UnitTestsCommon
{
public static IMapper AutoMapperInstance = null;
public UnitTestsCommon()
{
if(UnitTestsCommon.AutoMapperInstance == null){
Mapper.Initialize(AutoMapperConfiguration.BootstrapMapperConfiguration);
AutoMapperInstance = Mapper.Instance;
}
}
}
Try to use:
AutoMapper.Mapper.Configuration.AssertConfigurationIsValid();
It throws System.InvalidOperationException...Mapper not initialized. Call Initialize with appropriate configuration.
.
You could call Mapper.Reset();
before initializing your mapper. I do this when initializing my unit test classes:
[ClassInitialize]
public static void ClassInitializer(TestContext context)
{
Mapper.Reset();
AutoMapperDataConfig.Configure();
}