public void MigrateSucceeds() { var mockedMigrationAssembly = new Mock <IMigrationsAssembly>(); mockedMigrationAssembly.SetupGet(assembly => assembly.Migrations) .Returns(new Dictionary <string, TypeInfo> { { "", typeof(string).GetTypeInfo() } }); var mockedMigrator = new Mock <IMigrator>(); var mockedServiceProvider = new Mock <IServiceProvider>(); mockedServiceProvider.Setup(provider => provider.GetService(typeof(IMigrationsAssembly))) .Returns(() => mockedMigrationAssembly.Object); mockedServiceProvider.Setup(provider => provider.GetService(typeof(IMigrator))) .Returns(() => mockedMigrator.Object); var mockedContext = new Mock <DbContext>(); var mockedDatabase = new Mock <DatabaseFacade>(mockedContext.Object); mockedDatabase.As <IInfrastructure <IServiceProvider> >() .SetupGet(infrastructure => infrastructure.Instance) .Returns(() => mockedServiceProvider.Object); mockedContext.SetupGet(context => context.Database) .Returns(mockedDatabase.Object); var dbInitializer = new FakeDbInitializer(mockedContext.Object); Action action = () => dbInitializer.Seed(); action.Should().NotThrow(); mockedMigrator.Verify(migrator => migrator.Migrate(It.IsAny <string>()), Times.Once); mockedDatabase.Verify(database => database.EnsureCreated(), Times.Never); }
private static void CreateDbIfNotExists(IHost host) { using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var context = services.GetRequiredService <AcuityContext>(); FakeDbInitializer.Initialize(context); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred creating the DB."); } } }