-
-
Save pdevito3/192072c5b197c531337383aadb37af70 to your computer and use it in GitHub Desktop.
broken fixture factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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