Search code examples
c#unit-testingasp.net-identityvisual-studio-2017in-memory-database

ASP Core Identity - Unit test In Memory Error


I am trying to unit test a project of mine which uses ASP identity (EF core) within VS2017.

The issue I have is when I'm creating my DB context using EF cores in memory it blows up with the following Error -

Message: System.IO.FileLoadException : Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I'm using the following nuget packages

  • Microsoft.AspNetCore.Identity.EntityFrameworkCore - 1.1.2
  • Microsoft.EntityFrameworkCore.InMemory - 1.1.2

Rest are default xunit packages you get when creating the project.

Any help would be greatly appreciated, spend many hours trying to work out whats going wrong :/

This is the Unit Test Code:

public class TestDbContext : IdentityDbContext<User>
{
    public TestDbContext(DbContextOptions<TestDbContext> options) : base(options)
    {
    }
}

public class User : IdentityUser
{
}

[Fact]
public void Test1()
{
    var builder = new DbContextOptionsBuilder<TestDbContext>();

    builder.UseInMemoryDatabase();

    var context = new TestDbContext(builder.Options);
}

Solution

  • Upgrading to latest version of Microsoft.NET.Test.Sdk solves the problem.

    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />