Asp.net mvc AutoMapper+xUnit:缺少类型映射配置或映射不受支持
我搞不懂这个。我有一个N层ASP.MVC应用程序,我正在编写我的第一个单元测试,它在我的AutoMapper配置中似乎失败了。我已经使用AutoMapper一百万次了,使用它从来没有遇到任何问题 我肯定我错过了一些简单的东西,但我已经盯着这个看了24小时了 类库:APP.DOMAIN 我的AutoMapper引导程序位于服务层Asp.net mvc AutoMapper+xUnit:缺少类型映射配置或映射不受支持,asp.net-mvc,automapper,n-tier-architecture,xunit,Asp.net Mvc,Automapper,N Tier Architecture,Xunit,我搞不懂这个。我有一个N层ASP.MVC应用程序,我正在编写我的第一个单元测试,它在我的AutoMapper配置中似乎失败了。我已经使用AutoMapper一百万次了,使用它从来没有遇到任何问题 我肯定我错过了一些简单的东西,但我已经盯着这个看了24小时了 类库:APP.DOMAIN 我的AutoMapper引导程序位于服务层 public static class AutoMapperBootstrapper { public static void RegisterMappings(
public static class AutoMapperBootstrapper
{
public static void RegisterMappings()
{
Mapper.CreateMap<User, UserViewModel>();
}
}
单元测试层:
我不确定问题出在哪里,自从我上次使用AutoMapper已经有一段时间了,但我很确定以下方法会起作用:
return users.Select(Mapper.Map<UserViewModel>);
参加聚会有点晚,但是您在运行测试之前是否尝试过设置映射
public class UserServiceTest
{
public UserServiceTest()
{
// register the mappings before running the test
AutoMapperBootstrapper.RegisterMappings();
}
...
}
我们需要做的是注入自定义映射器Mock,如下所示。添加所有用于正在进行单元测试的特定类的自定义配置文件,并将ConfigureMapper插入到该类的构造函数中,该类需要IMapper对象
public IMapper ConfigureMapper()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<CustomProfile>();
cfg.AddProfile<UserCustomProfile>();
cfg.AddProfile<UserWorkProfile>();
});
return config.CreateMapper();
}
希望这能解决问题。我对这一行有问题:
var authorDTO = mapper.Map<AuthorCreationDTO>(AuthorinsideDB);
所以我改变了AutoMapper的版本
发件人:
到
版本=6.0.0
它起作用了。谢谢,但没有效果。仍然失败。您得到的异常是相同的吗?实际上,不是。-异常System.InvalidCastException:System.InvalidCastException:无法将类型为“WhereSelectListIterator2[App.Data.Model.User,App.Service.ViewModels.UserViewModels]”的对象强制转换为类型为“System.Collections.Generic.List1[App.Service.ViewModels.UserViewModel]”。在D:\Repositories\App\App.Service\Services\UserService.cs中的App.Service.Services.UserService.GetUsers:第38行,如果整个代码库依赖于静态自动映射器,该怎么办?
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"));
}
}
*** 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
return users.Select(Mapper.Map<UserViewModel>);
public class UserServiceTest
{
public UserServiceTest()
{
// register the mappings before running the test
AutoMapperBootstrapper.RegisterMappings();
}
...
}
public IMapper ConfigureMapper()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<CustomProfile>();
cfg.AddProfile<UserCustomProfile>();
cfg.AddProfile<UserWorkProfile>();
});
return config.CreateMapper();
}
var authorDTO = mapper.Map<AuthorCreationDTO>(AuthorinsideDB);
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />