Unit Testing with xUnit and AutoMapper

Photo by rawpixel on Unsplash
System.InvalidOperationException: 'Mapper already initialized. You must call Initialize once per application domain/process.'
using AutoMapper;public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
Mapper.Initialize(c => c.AddProfile<MappingProfile>());
}
}
using AutoMapper;public class WindowViewModel
{
private readonly IMapper mapper;
public WindowViewModel(ISomeService someService)
{
mapper = Mapper.Instance;
}
}
using AutoMapper;public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
Mapper = new MapperConfiguration(c =>
c.AddProfile<MappingProfile>()).CreateMapper();

}
}
using AutoMapper;public class WindowViewModel
{
private readonly IMapper mapper;
public WindowViewModel(ISomeService someService, IMapper mapper)
{
this.mapper = mapper;
}
}
public partial class DocumentsWindow : Window
{
public DocumentsWindow()
{
InitializeComponent();
var mapper = (Application.Current as App).Mapper;
ViewModel = new WindowViewModel(someService, mapper);
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Teng Wei Song

Teng Wei Song

My journey as a self-taught developer. Feel free to drop by my online profile at tengweisong.com as well!