public DbContextServiceTestBase() { // Tests are made possible for applications that depends on EFCore directly // because of the existence of InMemory database, no physical database is needed. // 1. InMemory is not a relational database provider. // 2. Constraints won't be enforced, e.g. referential integrity, non-nullable columns and so on. // However, the whole point is to unit test your application business logic, not EFCore! // So it is okay for the InMemory database to have the limitations mentioned above. // Database name is a requirement for InMemoryDatabase since .NET Core 2.2. // The database name needs to be unique to guarantee a fresh InMemory database, // i.e. even if a new context is created with the same Database name, the same // InMemory database will be used across the tests, this behaviour is undesirable. _databaseName = Guid.NewGuid().ToString(); var dbContextOptions = new DbContextOptionsBuilder <WebAPIDemoDbContext>() // Install-Package Microsoft.EntityFrameworkCore.InMemory .UseInMemoryDatabase(databaseName: _databaseName) .Options; _context = new WebAPIDemoDbContext(dbContextOptions); _context.Database.EnsureCreated(); // Leverage on existing transactional data initialiser to populate // the InMemory DbContext. DatabaseDataInitialiser.Initialise(_context); // Mocks are instantiated at the base for high reusability. // These instances can be setup individually in each test to fit // the context of the test. _mockSystemClock = new Mock <ISystemClock>(); _mockHttpContextAccessor = new Mock <IHttpContextAccessor>(); _unitOfWork = new UnitOfWork(_mockSystemClock.Object, _context, _mockHttpContextAccessor.Object); }
public UnitOfWorkTests() { _databaseName = Guid.NewGuid().ToString(); var dbContextOptions = new DbContextOptionsBuilder <WebAPIDemoDbContext>() // Install-Package Microsoft.EntityFrameworkCore.InMemory .UseInMemoryDatabase(databaseName: _databaseName) .Options; _context = new WebAPIDemoDbContext(dbContextOptions); _context.Database.EnsureCreated(); DatabaseDataInitialiser.Initialise(_context); // Refer to comments in DbContextServiceTestBase.cs _mockSystemClock = new Mock <ISystemClock>(); _mockHttpContextAccessor = new Mock <IHttpContextAccessor>(); _unitOfWorkSUT = new UnitOfWork(_mockSystemClock.Object, _context, _mockHttpContextAccessor.Object); }
public static IHost InitialiseDatabase(this IHost host) { using (var scope = host.Services.CreateScope()) { using (var context = scope.ServiceProvider.GetRequiredService <WebAPIDemoDbContext>()) { Log.Information("Running pending database migrations."); // Note the order in which the following methods are executed. // Migrate will ensure latest migrations and seed data is inserted before // the insertion of any dependent transactional data, e.g. User. // Ensures all remaining migrations are applied on application run. context.Database.Migrate(); Log.Information("Initialisting population of required transactional data."); // Ensures the database is correctly populated with proper initial transactional data. DatabaseDataInitialiser.Initialise(context); } } return(host); }