gpt4 book ai didi

asp.net-mvc - 自动映射器+xUnit : Missing type map configuration or unsupported mapping

转载 作者:行者123 更新时间:2023-12-02 11:58:26 29 4
gpt4 key购买 nike

我无法弄清楚这个问题。我有一个 N 层 ASP.MVC 应用程序,我正在编写我的第一个单元测试,它似乎在我的 AutoMapper 配置上失败。我已经使用 AutoMapper 一百万次了,从来没有遇到过任何问题。

我确信我错过了一些简单的东西,但我已经盯着它看了 24 小时了。

类库:APP.DOMAIN

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

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

类库:APP.SERVICE

引用App.Domain

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

我的 AutoMapper Bootstrap 位于服务层。

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层:APP.WEB

引用App.Service

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);
}

单元测试层:

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

最佳答案

有点晚了,但是您是否尝试在运行测试之前设置映射?

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

...
}

关于asp.net-mvc - 自动映射器+xUnit : Missing type map configuration or unsupported mapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25647838/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com