Пример #1
0
        public static async Task BuildServerAsync(
            Func <TestServer, Task> testAction = null,
            Dictionary <string, string> config = null,
            Action <IServiceCollection> configureServicesAction = null,
            string environmentName            = "Test",
            DateTimeAccessor dateTimeAccessor = null)
        {
            // string connString = $"Host=localhost;Database=identitybase_" +
            //      $"{Guid.NewGuid()};Username=postgres;Password=root";
            //
            Dictionary <string, string> configuration = config ??
                                                        ConfigBuilder.Default;

            //
            // configuration
            //     .Alter("EntityFramework:Npgsql:ConnectionString", connString);
            //
            // TestServerBuilder2.CreateSqlDatabase(connString, dateTimeAccessor);

            using (TestServer server = TestServerBuilder2.BuildServer(
                       configuration,
                       services =>
            {
                configureServicesAction?.Invoke(services);

                if (dateTimeAccessor != null)
                {
                    services
                    .AddSingleton <IDateTimeAccessor>(dateTimeAccessor);
                }
            },
                       environmentName))
            {
                try
                {
                    // Run the shit you call test
                    await testAction(server);
                }
                catch (Exception)
                {
                    // Let it burn if it crashes
                    throw;
                }
                finally
                {
                    // But cleanup the shit afterwards
                    // TestServerBuilder2.CleanUpSqlDatabase(connString);
                }
            }
        }
Пример #2
0
        public static TestServer BuildServer(
            Dictionary <string, string> config,
            Action <IServiceCollection> configureServicesAction,
            string environmentName = "Test")
        {
            // Create default configuration if none is passed
            IConfigurationRoot configuration = ConfigBuilder
                                               .Build(config ?? ConfigBuilder.Default);

            var contentRoot = TestServerBuilder2.GetContentRoot();
            var environment = new HostingEnvironment
            {
                EnvironmentName         = environmentName,
                ApplicationName         = "IdentityBase.Public",
                ContentRootPath         = contentRoot,
                ContentRootFileProvider = new PhysicalFileProvider(contentRoot)
            };

            var logger  = new NullLogger <Startup>();
            var startup = new Startup(configuration, environment, logger);

            var builder = new WebHostBuilder()
                          .UseContentRoot(contentRoot)
                          .UseEnvironment(environmentName)
                          .ConfigureServices(services =>
            {
                services.AddSingleton <IStartup>(startup);

                configureServicesAction?.Invoke(services);
            })
                          // WORKARROUND: https://github.com/aspnet/Hosting/issues/1137#issuecomment-323234886
                          .UseSetting(WebHostDefaults.ApplicationKey,
                                      typeof(Startup).GetTypeInfo().Assembly.FullName);

            return(new TestServer(builder));
        }