Skip to content

Instantly share code, notes, and snippets.

@pdevito3
Created April 15, 2020 13:36
Show Gist options
  • Save pdevito3/192072c5b197c531337383aadb37af70 to your computer and use it in GitHub Desktop.
Save pdevito3/192072c5b197c531337383aadb37af70 to your computer and use it in GitHub Desktop.
broken fixture factory
.ConfigureServices(services =>
{
// Create a new service provider.
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
// Add a database context using an in-memory
// database for testing.
services.AddDbContext<ValueToReplaceDbContext>(options =>
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
options.UseInternalServiceProvider(serviceProvider);
});
services.AddScoped(provider => provider.GetService<ValueToReplaceDbContext>());
var sp = services.BuildServiceProvider();
// Create a scope to obtain a reference to the database
using var scope = sp.CreateScope();
var scopedServices = scope.ServiceProvider;
var context = scopedServices.GetRequiredService<ValueToReplaceDbContext>();
// Ensure the database is created.
context.Database.EnsureCreated();
try
{
// Seed the database with test data.
//Utilities.InitializeDbForTests(context);
}
catch (Exception ex)
{
//logger.LogError(ex, "An error occurred seeding the " +
// $"database with test messages. Error: {ex.Message}");
}
})
.UseEnvironment("Test");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment