lundi 27 juin 2016

Missing type map configuration or unsupported mapping with Automapper 4.2 in SimpleInjector 3 In Unit Test

I am missing a type map configuration when running my unit test. It works fine though when I run the app. I am assuming that I have to register the mappings in the unit test project, but am not sure where to start with this. Anybody have any input on what I am missing would be greatly appreciated!

Global.asax

protected void Application_Start()
{
     var container = new Container();

     container.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();
     container.RegisterSingleton(Mapper.Engine);
     AutoMapperConfigurator.Configure();
     new SimpleInjectorConfigurator().RegisterSimpleInjectorInstances(container);

     container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

     container.Verify();

     GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);

     GlobalConfiguration.Configure(WebApiConfig.Register);
}

AutoMapperConfigurator

public class AutoMapperConfigurator
{
    public static void Configure()
    {

        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<UserModel, User>();
            cfg.CreateMap<User, UserModel>();
        });
    }
}

Example usage throwing the error (Works fine when I actually hit the api)

public UserModel Process(int userId)
{
    var response = _query.Execute(userId);

    var user = Mapper.Map<UserModel>(response);

    return user;
}

Aucun commentaire:

Enregistrer un commentaire