asp.net-mvc – AutoMapper xUnit:缺少类型映射配置或不支持的
发布时间:2020-12-16 09:54:39 所属栏目:asp.Net 来源:网络整理
导读:我无法想出这个.我有一个N层的ASP.MVC应用程序,我正在编写我的第一个单元测试,它似乎在我的AutoMapper配置上失败了.我已经使用了AutoMapper一百万次,并且从未使用过它. 我确定我错过了一些简单的东西,但我现在已经24小时盯着这个了. 类库:APP.DOMAIN public
我无法想出这个.我有一个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引导程序. 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:RepositoriesAppApp.ServiceServicesUserService.cs:line 36 at App.Tests.Service.Tests.UserServiceTest.GetUsers_Should_Return_Correct_Number_Of_Users() in D:RepositoriesAppApp.TestsService.TestsUserServiceTest.cs:line 34 解决方法
派对有点晚了但是你在试运行之前尝试过设置映射吗?
public class UserServiceTest { public UserServiceTest() { // register the mappings before running the test AutoMapperBootstrapper.RegisterMappings(); } ... } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 从azure云存储下载大文件的最佳方式
- ASP.Net – App_Data&App_Code文件夹?
- 您使用ASP.NET 2.0 webparts的经验如何?
- asp.net-mvc-5 – 在默认的MVC5应用程序的帐户关联步骤中,从
- 详解 ASP.NET Core MVC 的设计模式
- asp.net-mvc – 为什么我的View不包括_Layout.cshtml?
- asp.net-mvc-3 – 如何避免使用MVC3 FileContentResult重复
- asp.net-mvc – 如何从AuthorizationContext获取MVC Action
- ASP.NET Core 2.0 Identity中的Cookies.ApplicationCookie.
- asp.net – 何时使用Request.RegisterForDispose?
推荐文章
站长推荐
热点阅读