AutoMapper + xUnit: Missing type map configuration or unsupported display

I can not understand this. I have an ASP.MVC N-Tier application and I am writing my first Unit Test and does not seem to work in my AutoMapper configuration. I used AutoMapper a million times and never had any problems using it.

I am sure that I am missing something simple, but I have been looking at it for 24 hours.

Class Library: APP.DOMAIN

public class User : IEntity<int>
{
    public int Id { get; set; }

    [StringLength(20), Required]
    public string UserName { get; set; }
}

Class Library: APP.SERVICE

App.Domain Links

public class UserViewModel
{
    public int Id { get; set; }
    public string UserName { get; set; } 
}

I have AutoMapper autoload at the service level.

public static class AutoMapperBootstrapper
{
    public static void RegisterMappings()
    {
        Mapper.CreateMap<User, UserViewModel>();
    }
}

UserService.cs

 public class UserService : IUserService
 {
    private readonly IUserRepository _userRepository;

    public UserService(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }

    public List<UserViewModel> GetUsers()
    {
        var users = _userRepository.GetAll();

        if (users == null)
        {
            throw new Exception("No users found.");
        }

        return Mapper.Map<List<UserViewModel>>(users); // FAILS ON AUTOMAPPER
    }
  }

ASP.MVC Level: APP.WEB

App.Service Links

private void Application_Start(object sender, EventArgs e)
{
    // Register AutoMapper
    AutoMapperBootstrapper.RegisterMappings();
    Mapper.AssertConfigurationIsValid();

    // Code that runs on application startup
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

Unit Test Layer:

public class TestUserRepository :IUserRepository
{
    public IEnumerable<User> GetAll()
    {
        var users = new List<User>()
        {
            new User { Id = 1, UserName = "Mary"},
            new User { Id = 2, UserName = "Joe"}
        };
        return users;
    }
  }


public class UserServiceTest
{
    private IUserService _userService;
    private readonly IUserRepository _userRepository;

    public UserServiceTest()
    {
        _userRepository = new TestUserRepository();
    }

    [Fact]
    public void GetUsers_Should_Return_Correct_Number_Of_Users()
    {
        // Arrange
        _userService = new UserService(_userRepository);

        // Act
        var result = _userService.GetUsers(); // FAILS ON AUTOMAPPER

        // Assert
        Assert.True(result.Any(u => u.UserName == "Mary")); 
    }
}

Failed test message:

*** Failures ***

Exception
AutoMapper.AutoMapperMappingException: AutoMapper.AutoMapperMappingException : Missing type map configuration or unsupported mapping.

Mapping types:
User -> UserViewModel
App.Data.Model.User -> App.Service.ViewModels.UserViewModel

Destination path:
List`1[0]

Source value:
App.Data.Model.User
   at App.Service.Services.UserService.GetUsers() in D:\Repositories\App\App.Service\Services\UserService.cs:line 36
   at App.Tests.Service.Tests.UserServiceTest.GetUsers_Should_Return_Correct_Number_Of_Users() in D:\Repositories\App\App.Tests\Service.Tests\UserServiceTest.cs:line 34
+4
source share
3

, ?

public class UserServiceTest
{    
    public UserServiceTest() 
    {
        // register the mappings before running the test
        AutoMapperBootstrapper.RegisterMappings();
    }

    ...
}
+2

Inject Custom Mapper Mock, . , , , ConfigureMapper() , IMapper

public IMapper ConfigureMapper()
{
    var config = new MapperConfiguration(cfg =>
    {
        cfg.AddProfile<CustomProfile>();
        cfg.AddProfile<UserCustomProfile>();
        cfg.AddProfile<UserWorkProfile>(); 
    });
    return config.CreateMapper();
}

, .

+1

, , , AutoMapper, , :

return users.Select(Mapper.Map<UserViewModel>);
0

All Articles