无法在单元测试中设置AutoMapper,无法加载文件或程序集Microsoft.AspNetCore.Mvc.ViewFeatures

编程入门 行业动态 更新时间:2024-10-27 13:34:05
本文介绍了无法在单元测试中设置AutoMapper,无法加载文件或程序集Microsoft.AspNetCore.Mvc.ViewFeatures的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在.Net Core 2.1 Razor Pages项目中使用xUnit创建一个单元测试类,并且似乎在创建AutoMapper IMapper 对象以传递回我的方法时遇到麻烦要测试.

I'm trying to create a Unit Test class using xUnit in my .Net Core 2.1 Razor Pages project and I seem to be having trouble creating an AutoMapper IMapper object to pass back to the methods I want to test.

这是 MainProject.csproj/AutoMapper/DomainProfile.cs

using AutoMapper; using MainProject.Models; using MainProject.Models.ViewModels; namespace MainProject.AutoMapper { // Ref: dotnetcoretutorials/2017/09/23/using-automapper-asp-net-core/ // AutoMapper (called from Startup.cs) will search for any class that inherits from Profile public class DomainProfile : Profile { public DomainProfile() { CreateMap<MyModel, MyViewModel>() .ForMember(vm => vm.Property1, map => map.MapFrom(m => m.Property1)) .ForMember(vm => vm.Property2, map => map.MapFrom(m => m.Property2)) .ReverseMap(); } } }

不确定是否重要,但这是我的 MainProject.csproj/Startup.cs 的 ConfigureServices 方法,其中设置了AutoMapper的依赖项注入:

Unsure if it matters, but here is my MainProject.csproj/Startup.cs's ConfigureServices method where the Dependency Injection for AutoMapper get's setup:

public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { ... }); services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>(); // AutoMapper services.AddAutoMapper(); services.AddMvc() .AddRazorPagesOptions(options => { ... }) .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); }

最后,这是我一直试图开始工作的精简测试类, MainProject.Test.csproj/Services/MyModelServicesTest.cs :

Finally, here is my stripped down test class I've been trying to get working, MainProject.Test.csproj/Services/MyModelServicesTest.cs:

using System.Threading.Tasks; using AutoMapper; using Outreach.AutoMapper; using Xunit; ... ... namespace MainProject.Test.Services { public class MyModelServicesTest { [Fact] public async Task MyUnitTestMethod() { var config = new MapperConfiguration(cfg => { cfg.AddProfile(new DomainProfile()); }); var mapper = config.CreateMapper(); // myModelServices.SomeMethod(mapper); // ... Other code that is never reached before the error throws } } }

当代码到达 cfg.AddProfile(new DomainProfile()); 时,它将引发以下错误:

When the code hits cfg.AddProfile(new DomainProfile()); it throws this error:

System.IO.FileNotFoundException:无法加载文件或程序集'Microsoft.AspNetCore.Mvc.ViewFeatures,版本= 2.1.1.0,区域性=中性,PublicKeyToken = adb9793829ddae60'.系统找不到指定的文件.

System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

有什么建议吗?

推荐答案

System.IO.FileNotFoundException:无法加载文件或程序集'Microsoft.AspNetCore.Mvc.ViewFeatures,版本= 2.1.1.0,文化=中性,PublicKeyToken = adb9793829ddae60'.系统无法找到指定的文件.

System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

根据错误消息,可能是缺少 AspNetCore 依赖性.例如,2.0中的 Microsoft.AspNetCore.All 或2.1中的 Microsoft.AspNetCore.App .

According to the error message, it could be that AspNetCore dependency is missing. For example, Microsoft.AspNetCore.All in 2.0 or Microsoft.AspNetCore.App in 2.1.

我通常将AutoMapper注入Controller.然后使用以下方法对控制器进行单元测试.

I normally inject AutoMapper to Controller. Then use the following approach to unit test a controller.

public class MyModelServicesTest { private readonly Mapper _mapper; public MyModelServicesTest() { _mapper = new Mapper(new MapperConfiguration(cfg => cfg.AddProfile(new MappingProfile()))); } [Fact] public async Task MyUnitTestMethod() { var sut = new SomeController(_mapper, _mockSomeRepository.Object); } }

更多推荐

无法在单元测试中设置AutoMapper,无法加载文件或程序集Microsoft.AspNetCore.Mvc.ViewFeatures

本文发布于:2023-11-16 10:17:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元   加载   文件   程序   测试中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!