コード例 #1
0
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            SetupEnvironmentVariables();

            builder.ConfigureServices(services =>
            {
                RemoveDefaultDbContextFromServiceCollection(services);
                RemoveDefaultDistributedCacheFromServiceCollection(services);

                services.AddDistributedMemoryCache();

                services.AddDbContext <SurveyContext>(options =>
                {
                    options.UseInMemoryDatabase("InMemoryDbForTesting");
                });

                ConfigureMockServices(services);

                var rootServiceProvider = services.BuildServiceProvider();

                using var scope = rootServiceProvider.CreateScope();

                var scopedServiceProvider = scope.ServiceProvider;
                var context = scopedServiceProvider.GetRequiredService <SurveyContext>();
                var logger  = scopedServiceProvider
                              .GetRequiredService <ILogger <IntegrationTestWebApplicationFactory <TStartup> > >();

                var cache = scopedServiceProvider.GetRequiredService <IDistributedCache>();

                cache.Remove("FakeSurveyGenerator");

                context.Database.EnsureCreated();

                try
                {
                    DatabaseSeed.SeedSampleData(context);
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "An error occurred seeding the database with test surveys. Error: {Message}",
                                    ex.Message);
                }
            });
        }
コード例 #2
0
 public static void SeedSampleData(SurveyContext context)
 {
     DatabaseSeed.SeedSampleData(context);
 }
コード例 #3
0
 public static async Task SeedSampleData(SurveyContext context)
 {
     await DatabaseSeed.SeedSampleData(context);
 }