79160037

Date: 2024-11-05 17:05:39
Score: 0.5
Natty:
Report link

I've seen in this video that mocking automapper its not a good approach: https://www.youtube.com/watch?v=RsnEZdc3MrE

Instead, you can use a Mapper creator and return your real application mapper.

In the test project:

//this is going to return your real mapper    
var mapper = MapperBuilder.Create();

--

public static class MapperBuilder
{
    public static IMapper Create()
    {
        return new AutoMapper.MapperConfiguration(options =>
        {
            options.AddProfile(new MyRealMapper());
        }).CreateMapper();
    }
}

In your Application project:

public class MyRealMapper : Profile
{
    public MyRealMapper()
    {
        RequestToDomain();
        DomainToResponse();
    }

    private void DomainToResponse()
    {
        CreateMap<User, ResponseUserCreateJson>();
        CreateMap<User, ResponseUserChangePasswordJson>();
    }

    private void RequestToDomain()
    {
        CreateMap<RequestUserCreateJson, User>()
            .ForMember(u => u.Password, config => config.Ignore());
    }
}
Reasons:
  • Blacklisted phrase (1): youtube.com
  • Blacklisted phrase (1): this video
  • Whitelisted phrase (-1.5): you can use
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: dama-dev