Reputation: 839
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?
Upvotes: 6
Views: 3757
Reputation: 1568
In my case I added
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.1" />
To the xUnit project file and
rebuild the solution and unit tests didn't give me that error anymore.
Upvotes: 2
Reputation: 62260
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.
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.
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);
}
}
Upvotes: 1
Reputation: 26765
Try explicitly passing the type in for AutoMapper:
services.AddAutoMapper(typeof(Startup), ///additional types where you can find profiles));
Without the type, the extension will scan loaded assemblies. That seems to cause problems in unit tests, so to be safe be explicit about where to look for Profile
instances.
Upvotes: 3