title: Unable to setup AutoMapper in Unit Test, Could not load file or assembly Microsoft.AspNetCore.Mvc.ViewFeatures
body: <p>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 <code>IMapper</code> object to pass back to the methods I want to test.</p>
  
  <p>Here is a simplified version of my AutoMapper profile in <code>MainProject.csproj/AutoMapper/DomainProfile.cs</code></p>
  
  <pre class="lang-cs prettyprint-override"><code>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&lt;MyModel, MyViewModel&gt;()
                  .ForMember(vm =&gt; vm.Property1, map =&gt; map.MapFrom(m =&gt; m.Property1))
                  .ForMember(vm =&gt; vm.Property2, map =&gt; map.MapFrom(m =&gt; m.Property2))
                  .ReverseMap();
          }
      }
  }
  </code></pre>
  
  <p>Unsure if it matters, but here is my <code>MainProject.csproj/Startup.cs</code>'s <code>ConfigureServices</code> method where the Dependency Injection for AutoMapper get's setup:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public void ConfigureServices(IServiceCollection services)
  {
      services.Configure&lt;CookiePolicyOptions&gt;(options =&gt; { ... });
  
      services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt; 
          options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
      services.AddDefaultIdentity&lt;IdentityUser&gt;().AddEntityFrameworkStores&lt;ApplicationDbContext&gt;();
  
      // AutoMapper
      services.AddAutoMapper();
  
      services.AddMvc()
          .AddRazorPagesOptions(options =&gt; { ... })
          .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  }
  </code></pre>
  
  <p>Finally, here is my stripped down test class I've been trying to get working, <code>MainProject.Test.csproj/Services/MyModelServicesTest.cs</code>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>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 =&gt;
              {
                  cfg.AddProfile(new DomainProfile());
              });
              var mapper = config.CreateMapper();
  
              // myModelServices.SomeMethod(mapper);
              // ... Other code that is never reached before the error throws
          }
      }
  }
  </code></pre>
  
  <p>When the code hits <code>cfg.AddProfile(new DomainProfile());</code> it throws this error:</p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>Any suggestions?</p>