Repository navigation
Closed
Labels
Milestone
Description
The Issue
For testing purposes, you should be able to delete, recreate, and reseed InMemory databases and the result should be the same for each test. Currently identity columns do not reset, so IDs increment with each test iteration.
Repro Steps
This test fails. It should pass.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace EfCoreInMemory
{
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<Item> Items { get; set; }
public AppDbContext(DbContextOptions<AppDbContext> options ):base(options)
{
}
}
public class Tests
{
private static DbContextOptions<AppDbContext> CreateNewContextOptions()
{
// Create a fresh service provider, and therefore a fresh
// InMemory database instance.
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
// Create a new options instance telling the context to use an
// InMemory database and the new service provider.
var builder = new DbContextOptionsBuilder<AppDbContext>();
builder.UseInMemoryDatabase()
.UseInternalServiceProvider(serviceProvider);
return builder.Options;
}
[Fact]
public void Test1()
{
// create a brand new dbContext
var dbContext = new AppDbContext(CreateNewContextOptions());
// add one item
var item = new Item();
dbContext.Items.Add(item);
dbContext.SaveChanges();
// ID should be 1
Assert.Equal(1, item.Id);
dbContext.Database.EnsureDeleted();
Assert.False(dbContext.Items.Any());
var item2 = new Item();
dbContext.Items.Add(item2);
dbContext.SaveChanges();
// ID should STILL be 1
Assert.Equal(1, item2.Id);
}
}
}Further technical details
Project.json:
{
"version": "1.0.0-*",
"testRunner": "xunit",
"dependencies": {
"NETStandard.Library": "1.6.0",
"xunit": "2.2.0-beta2-build3300",
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.AspNetCore.TestHost": "1.0.0",
"Microsoft.EntityFrameworkCore": "1.0.0",
"Microsoft.EntityFrameworkCore.InMemory": "1.0.0"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6"
],
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
}
}
}
}VS2015
Reacted by Ryan Stelly, paul-mercer, David Menoud, Paul Lucas, Martin Jul Hammer, Igoreso, Simon Cropp, Roman Marusyk, Bernhard Koenig, Sergi Papaseit and 28 more
Activity
robertmclaws commentedon Oct 26, 2016
I don't know if I agree with this exact behavior, mostly because I'm having a hard time envisioning a unit test or operational scenario where you'd want to delete an existing item, then add a new item with the same ID.
HOWEVER, I do see the need for a command like
dbContext.ResetIdTracking()that frees up any IDs not presently in the change tracker. You could then in this command at the end of each test function to reset the context for the next test.ardalis commentedon Oct 26, 2016
The repel shows one test. The problem exhibits itself when you have many tests. Every test will end up with different IDs - there is now way to really reset the in memory db.
Steve Smith
ardalis commentedon Oct 26, 2016
repro not repel
ajcvickers commentedon Oct 26, 2016
Note for triage: dupe of #4096.
rowanmiller commentedon Oct 31, 2016
The InMemory provider doesn't the Identity pattern you would get in a relational database. When a column is configured as
ValueGeneratedOnAddit uses values from a single generator for all tables. This would be similar to having all tables setup to use a single sequence in a relational database. This raises one of the important design principles of our InMemory provider, which is that we are not trying to emulate a relational database. This becomes more important as we start to light up non-relational providers.If you want to have a database that acts like a relational database, but doesn't have the overhead of I/O, then we'd recommend using an In-Memory SQLite database - http://www.sqlite.org/inmemorydb.html.
We're going to update our testing docs to make the SQLite option more prominent.
ardalis commentedon Oct 31, 2016
Thanks, @rowanmiller
2 things I'd like to see that I think would be useful:
Thanks!
rowanmiller commentedon Oct 31, 2016
@ardalis fair point on point 1, I'm re-opening this issue for us to re-discuss.
Regarding point 2, our docs show an example that tests a
BlogService, which would be the same as testing an MVC controller etc. - https://docs.efproject.net/en/latest/miscellaneous/testing.html. For testing, you generally wouldn't be using Startup.cs to setup all your services, you'd be creating your test doubles of everything the controller depends on and passing them in. The example I linked to shows how to do that for EF, but you'd have to do it for the other dependencies too.robertmclaws commentedon Oct 31, 2016
I do want to point out that I made a suggestion on how to handle what @ardalis is asking for in point 1 with a command like
dbContext.ResetIdTracking(), or something to that effect.ardalis commentedon Oct 31, 2016
@rowanmiller You're right, unit testing an individual controller action works just like your BlogService sample. To clarify, I'm talking about integration testing using WebHostBuilder, as shown here: https://docs.asp.net/en/latest/testing/integration-testing.html. In this case, there's no direct access to the controller action. The web host is constructed for each test and the request is made such that it goes through the full MVC stack (routing, filters, etc). This is an awesome new feature of ASP.NET Core but getting the data reset for each test has proven much more difficult than I would like. To be fair, I haven't yet resorted to using SqlLite for this purpose, but my hope has been to avoid the need for this since it adds complexity to the process. Thanks.
ardalis commentedon Oct 31, 2016
...and regarding that link, you'll see it doesn't include any EF examples. That's because I couldn't figure a great way to include them such that they were reset between each test.
ardalis commentedon Oct 31, 2016
Here's a complete example showing how to (try to) configure an ASP.NET Core app using WebHostBuilder and InMemory for integration tests: https://github.com/ardalis/CleanArchitecture/blob/master/src/CleanArchitecture.Web/Startup.cs#L73-L104
The sample shown works, but starts to fall down as you add more entities and tests to it, as test data starts to step on other test data. One workaround I was able to use was hardcoding the IDs of test data, as suggested in another issue. However, any guidance or assistance with how to reset data for each integration test like this would be appreciated.
61 remaining items
saliksaly commentedon Feb 6, 2020
@ajcvickers, please - I have been using your solution to set id value generators for my in-emory tests with seed data. Now, in EF Core 3.1, it stoped working. It seems that ResettableValueGenerator.Next() is newer called.
Again, I get error: System.InvalidOperationException: 'The instance of entity type X cannot be tracked because another instance with the key value '{Id: 1}' is already being tracked.
Would not you know solution for EF Core 3.1?
Thanks
ajcvickers commentedon Feb 10, 2020
@saliksaly It should no longer be necessary to use a workaround here since the underlying issues were fixed in 3.0. If you're running into issues with in-memory keys, then please open a new issue and include a small, runnable project or complete code listing so that we can investigate.
saliksaly commentedon Feb 11, 2020
Thanks, here is the issue: #19854