Exemplo n.º 1
0
        private TestServer CreateServer(SystemUnderTest <T> systemUnderTest, SystemTopology topology)
        {
            var config            = new[] { new Action <IConfiguration>(systemUnderTest.Configuration) };
            var configureServices = new[] { new Action <IServiceCollection>(systemUnderTest.ConfigureServices) };
            var configure         = new[]
            {
                BeforeConfigure,
                new Action <IApplicationBuilder>(systemUnderTest.Configure)
            };

            var builder = CreateWebHostBuilder(config, configureServices, configure, topology);

            return(new TestServer(builder));
        }
Exemplo n.º 2
0
 public SystemHostFixture(SystemUnderTest <T> systemUnderTest, SystemTopology topology)
 {
     _systemUnderTest = systemUnderTest;
     _topology        = topology;
     _server          = new Lazy <TestServer>(() => CreateServer(systemUnderTest, topology));
 }
Exemplo n.º 3
0
        private IWebHostBuilder CreateWebHostBuilder
        (
            ICollection <Action <IConfiguration> > configuration,
            ICollection <Action <IServiceCollection> > configureServices,
            ICollection <Action <IApplicationBuilder> > configure,
            SystemTopology topology)
        {
            T startup = null;
            IConfiguration      config   = null;
            IServiceCollection  services = null;
            IWebHostEnvironment env;

            var builder = new WebHostBuilder()
                          .ConfigureAppConfiguration((context, cb) =>
            {
                env = context.HostingEnvironment;
                var testSettings = BuildTestSettings(env.ContentRootPath, env.EnvironmentName);
                ConfigureAppConfiguration(env, testSettings, cb);
                config = cb.Build();
            })
                          .ConfigureServices(serviceCollection =>
            {
                services = serviceCollection;

                Debug.Assert(services != null);
                Debug.Assert(config != null);

                var serviceProvider = serviceCollection.BuildServiceProvider();
                Debug.Assert(serviceProvider != null);

                var container = new DependencyContainer(serviceProvider);

                container.Register(serviceProvider);
                container.Register(services);
                container.Register(config);

                startup = container.Resolve <T>();
                Debug.Assert(startup != null);
                foreach (var action in configuration)
                {
                    action(config);
                }

                container.Register(startup);

                var method = startup.GetType().GetMethod("ConfigureServices");
                if (method != null)
                {
                    var accessor = CallAccessor.Create(method);
                    accessor.Call(startup);
                }

                foreach (var action in configureServices)
                {
                    action(serviceCollection);
                }
            });

            if (topology == SystemTopology.Web)
            {
                builder.Configure(app =>
                {
                    Debug.Assert(services != null);
                    Debug.Assert(config != null);
                    Debug.Assert(startup != null);
                    Debug.Assert(app != null);

                    var serviceProvider = services.BuildServiceProvider();
                    Debug.Assert(serviceProvider != null);

                    var container = new DependencyContainer(serviceProvider);

                    container.Register(serviceProvider);
                    container.Register(services);
                    container.Register(config);
                    container.Register(startup);
                    container.Register(app);

                    var method = startup.GetType().GetMethod("Configure");
                    if (method != null)
                    {
                        var accessor = CallAccessor.Create(method);
                        accessor.Call(startup);
                    }
                    foreach (var action in configure)
                    {
                        action(app);
                    }
                });
            }

            UseAssemblyForSetting <T>(builder, WebHostDefaults.ApplicationKey);
            UseAssemblyForSetting <T>(builder, WebHostDefaults.StartupAssemblyKey);

            Services = services;

            return(builder);
        }