Exemple #1
0
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.ConfigureServices(services =>
            {
                var descriptor = services.Where(
                    d => ToRemove.Contains(d.ServiceType)).ToList();

                foreach (var d in descriptor)
                {
                    services.Remove(d);
                }

                // Add ApplicationDbContext using an in-memory database for testing.
                services.AddDbContext <CodeGolfContext>(options =>
                {
                    options.UseInMemoryDatabase("InMemoryDbForTesting");
                });
                services.AddSingleton <IGetIp, GetMockIp>();
                services.AddSingleton <IRecaptchaVerifier, MockRecaptchaVerifier>();

                // Build the service provider.
                var sp = services.BuildServiceProvider();

                // Create a scope to obtain a reference to the database
                // context (ApplicationDbContext).
                using (var scope = sp.CreateScope())
                {
                    var scopedServices = scope.ServiceProvider;
                    var db             = scopedServices.GetRequiredService <CodeGolfContext>();
                    var logger         = scopedServices
                                         .GetRequiredService <ILogger <CustomWebApplicationFactory <TStartup> > >();

                    // Ensure the database is created.
                    db.Database.EnsureCreated();

                    try
                    {
                        // Seed the database with test data.
                        DbInitialiser.InitializeDbForTests(db);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(
                            ex,
                            "An error occurred seeding the database with test messages. Error: {Message}",
                            ex.Message);
                    }
                }
            });

            builder.ConfigureAppConfiguration((_, configurationBuilder) =>
                                              configurationBuilder.AddInMemoryCollection(new[]
            {
                new KeyValuePair <string, string>("GitHub:ClientId", "aaa"),
                new KeyValuePair <string, string>("GitHub:ClientSecret", "aaa"),
                new KeyValuePair <string, string>("DbPath", "Data Source=codeGolf.db"),
                new KeyValuePair <string, string>("Execution:UseRemoteService", "false"),
                new KeyValuePair <string, string>("AdminGithubNames:0", "luhis"),
            }));
        }
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.ConfigureServices(services =>
            {
                var descriptor = services.Where(
                    d => ToRemove.Contains(d.ServiceType)).ToList();

                foreach (var d in descriptor)
                {
                    services.Remove(d);
                }

                // Add ApplicationDbContext using an in-memory database for testing.
                services.AddDbContext <MyRentalsContext>(options =>
                {
                    options.UseInMemoryDatabase("InMemoryDbForTesting");
                });

                // Build the service provider.
                var sp = services.BuildServiceProvider();

                // Create a scope to obtain a reference to the database
                // context (ApplicationDbContext).
                using (var scope = sp.CreateScope())
                {
                    var scopedServices = scope.ServiceProvider;
                    var db             = scopedServices.GetRequiredService <MyRentalsContext>();
                    var logger         = scopedServices
                                         .GetRequiredService <ILogger <CustomWebApplicationFactory <TStartup> > >();

                    // Ensure the database is created.
                    db.Database.EnsureCreated();

                    try
                    {
                        // Seed the database with test data.
                        DbInitialiser.InitializeDbForTests(db);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(
                            ex,
                            "An error occurred seeding the database with test messages. Error: {Message}",
                            ex.Message);
                    }
                }
            });

            builder.ConfigureAppConfiguration((_, configurationBuilder) =>
                                              configurationBuilder.AddInMemoryCollection(new[]
            {
                new KeyValuePair <string, string>("DbPath", "Data Source=myRentalsTest.db"),
                new KeyValuePair <string, string>("AdminEmails:0", "*****@*****.**"),
            }));
        }
Exemple #3
0
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.ConfigureServices(services =>
            {
                var descriptor = services.Where(
                    d => ToRemove.Contains(d.ServiceType)).ToList();

                foreach (var d in descriptor)
                {
                    services.Remove(d);
                }

                // Add ApplicationDbContext using an in-memory database for testing.
                services.AddDbContext <AutoTestContext>(options =>
                {
                    options.UseInMemoryDatabase("InMemoryDbForTesting");
                });

                // Build the service provider.
                var sp = services.BuildServiceProvider();

                // Create a scope to obtain a reference to the database
                // context (ApplicationDbContext).
                using (var scope = sp.CreateScope())
                {
                    var scopedServices = scope.ServiceProvider;
                    var db             = scopedServices.GetRequiredService <AutoTestContext>();
                    var logger         = scopedServices
                                         .GetRequiredService <ILogger <CustomWebApplicationFactory <TStartup> > >();

                    // Ensure the database is created.
                    db.Database.EnsureCreated();

                    try
                    {
                        // Seed the database with test data.
                        DbInitialiser.InitializeDbForTests(db);
                    }
                    catch (Exception ex)
                    {
                        logger.LogError(
                            ex,
                            "An error occurred seeding the database with test messages. Error: {Message}",
                            ex.Message);
                    }
                }
            });
        }