Base solution for your next web application
Open Closed

ProjectTo in unit tests #3890


User avatar
0
TomLeonard created

Hello

could you help with the unit tests issue it works in product and unit tests:

var tags = await _tagRepository.GetAll().ToListAsync();
var tagsDto = ObjectMapper.Map<List<TagDto>>(tags);

but if I want to optimize for projections like this

var tagsDto = await _tagRepository.GetAll().ProjectTo<TagDto>().ToListAsync();

it asks for IConfigurationProvider in unit tests (but ok in product).

Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.

Could you advise how to setup it in tests.

Thanks!


2 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Pass in an IConfigurationProvider instance:

    var config = new MapperConfiguration(configuration =>
    {
        configuration.CreateAutoAttributeMaps(typeof(TagDto));
    });
    
    var tagsDto = await _tagRepository.GetAll().ProjectTo<TagDto>(config).ToListAsync();
    
  • User Avatar
    0
    TomLeonard created

    Thanks!