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.
Here is a simplified version of my AutoMapper profile in 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();
}
}
}
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);
}
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
}
}
}
When the code hits cfg.AddProfile(new DomainProfile()); it throws this error:
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.
Any suggestions?