I have migrated a web application project from .NET Core 2.1 to 3.1 (also EF Core from 2.1.1 to 3.1.0).
After the migration, some unit tests are not working anymore, throwing duplicate keys db exception.
I simulated the problem and realize that EF core with option UseInMemoryDatabase is behaving differently in 3.1, it does not clean up the old data.
In the second test method, the People table already contains data added from the first test, which is not happening in 2.1
Does anyone know how can I make in-memory database to be scoped to each unit test?
Here is my testing code:
AppDbContext.cs
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace MyConsoleApp.Database
{
public class AppDbContext: DbContext
{
protected AppDbContext(DbContextOptions options) : base(options) { }
public AppDbContext(DbContextOptions<AppDbContext> options) : this((DbContextOptions)options)
{
}
public virtual DbSet<Person> Person { get; set; }
}
public class Person
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
}
AppUnitTest.cs
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyConsoleApp.Database;
using System.Linq;
namespace MyConsoleAppTest
{
[TestClass]
public class AppUnitTest
{
public ServiceCollection Services { get; private set; }
public ServiceProvider ServiceProvider { get; protected set; }
[TestInitialize]
public void Initialize()
{
Services = new ServiceCollection();
Services.AddDbContext<AppDbContext>(opt => opt.UseInMemoryDatabase(databaseName: "InMemoryDb"),
ServiceLifetime.Scoped,
ServiceLifetime.Scoped);
ServiceProvider = Services.BuildServiceProvider();
}
[TestMethod]
public void TestMethod1()
{
using (var dbContext = ServiceProvider.GetService<AppDbContext>())
{
dbContext.Person.Add(new Person { Id = 0, Name = "test1" });
dbContext.SaveChanges();
Assert.IsTrue(dbContext.Person.Count() == 1);
}
}
[TestMethod]
public void TestMethod2()
{
using (var dbContext = ServiceProvider.GetService<AppDbContext>())
{
dbContext.Person.Add(new Person { Id = 0, Name = "test2" });
dbContext.SaveChanges();
Assert.IsTrue(dbContext.Person.Count() == 1);
}
}
[TestCleanup]
public virtual void Cleanup()
{
ServiceProvider.Dispose();
ServiceProvider = null;
}
}
}
MyConsoleAppTest.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.0.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyConsoleApp\MyConsoleApp.csproj" />
</ItemGroup>
</Project>
I would personally build a service-provider for each test so you'll make sure that there is no shared-state between tests that are being executed simultaneously. Something like this:
Then use this function to build the provider in each test
This might cause the execution time to be a little bit higher than before but should definitely prevent your current problem from happening again.
Tip:
You could also use the c# 8 syntax using statements now since you are running on
netcoreapp3.1