无法在单元测试中设置AutoMapper,无法加载文件或程序集Microsoft.AspNetCore.Mvc.ViewFeatures [英] Unable to setup AutoMapper in Unit Test, Could not load file or assembly Microsoft.AspNetCore.Mvc.ViewFeatures

查看:40
本文介绍了无法在单元测试中设置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: https://dotnetcoretutorials.com/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆