Esempio n. 1
0
        private static void AddTestServices(
            IServiceCollection services,
            string applicationWebSiteName,
            string applicationPath,
            Action <IServiceCollection> configureServices)
        {
            applicationPath = applicationPath ?? TestProjectsPath;

            // Get current IApplicationEnvironment; likely added by the host.
            var provider            = services.BuildServiceProvider();
            var originalEnvironment = provider.GetRequiredService <IApplicationEnvironment>();

            // When an application executes in a regular context, the application base path points to the root
            // directory where the application is located, for example MvcSample.Web. However, when executing
            // an application as part of a test, the ApplicationBasePath of the IApplicationEnvironment points
            // to the root folder of the test project.
            // To compensate for this, we need to calculate the original path and override the application
            // environment value so that components like the view engine work properly in the context of the
            // test.
            var applicationBasePath = CalculateApplicationBasePath(
                originalEnvironment,
                applicationWebSiteName,
                applicationPath);
            var environment = new TestApplicationEnvironment(
                originalEnvironment,
                applicationBasePath);

            services.AddInstance <IApplicationEnvironment>(environment);
            services.AddInstance <IHostingEnvironment>(new HostingEnvironment(environment));
            var providerType  = CreateAssemblyProviderType(applicationWebSiteName);
            var configuration = new TestConfigurationProvider();

            configuration.Configuration.Set(
                typeof(IAssemblyProvider).FullName,
                providerType.AssemblyQualifiedName);
            services.AddInstance <ITestConfigurationProvider>(configuration);
            services.AddInstance <ILoggerFactory>(new LoggerFactory());


            if (configureServices != null)
            {
                configureServices(services);
            }
        }
Esempio n. 2
0
        protected TestServer CreateServer()
        {
            EnsurePath(Path.Combine(TestProjectsPath, TemplateName, "wwwroot"));

            // Get current IApplicationEnvironment; likely added by the host.
            var provider = CallContextServiceLocator.Locator.ServiceProvider;
            var originalEnvironment = provider.GetRequiredService<IApplicationEnvironment>();

            // When an application executes in a regular context, the application base path points to the root
            // directory where the application is located, for example MvcSample.Web. However, when executing
            // an application as part of a test, the ApplicationBasePath of the IApplicationEnvironment points
            // to the root folder of the test project.
            // To compensate for this, we need to calculate the original path and override the application
            // environment value so that components like the view engine work properly in the context of the
            // test.
            var applicationBasePath = CalculateApplicationBasePath(
                originalEnvironment,
                TemplateName,
                TestProjectsPath);
            var environment = new TestApplicationEnvironment(
                originalEnvironment,
                applicationBasePath,
                TemplateName);

            var hostingEnvironment = new HostingEnvironment();
            hostingEnvironment.Initialize(applicationBasePath, environmentName: null);
            try
            {
                CallContextServiceLocator.Locator.ServiceProvider = new WrappingServiceProvider(provider, environment, hostingEnvironment);
                var assemblyProvider = CreateAssemblyProvider(TemplateName);
                var builder = TestServer.CreateBuilder()
                    .UseStartup(TemplateName)
                    .UseServices(services => 
                    {
                        services.AddInstance<IHostingEnvironment>(hostingEnvironment);
                        services.AddInstance(assemblyProvider);
                    });
                return new TestServer(builder);
            }
            finally
            {
                CallContextServiceLocator.Locator.ServiceProvider = provider;
            }
        }
        private static void AddTestServices(
            IServiceCollection services,
            string applicationWebSiteName,
            string applicationPath,
            Action<IServiceCollection> configureServices)
        {
            applicationPath = applicationPath ?? TestProjectsPath;

            // Get current IApplicationEnvironment; likely added by the host.
            var provider = services.BuildServiceProvider();
            var originalEnvironment = provider.GetRequiredService<IApplicationEnvironment>();

            // When an application executes in a regular context, the application base path points to the root
            // directory where the application is located, for example MvcSample.Web. However, when executing
            // an application as part of a test, the ApplicationBasePath of the IApplicationEnvironment points
            // to the root folder of the test project.
            // To compensate for this, we need to calculate the original path and override the application
            // environment value so that components like the view engine work properly in the context of the
            // test.
            var applicationBasePath = CalculateApplicationBasePath(
                originalEnvironment,
                applicationWebSiteName,
                applicationPath);
            var environment = new TestApplicationEnvironment(
                originalEnvironment,
                applicationBasePath);
            services.AddInstance<IApplicationEnvironment>(environment);
            services.AddInstance<IHostingEnvironment>(new HostingEnvironment(environment));
            var providerType = CreateAssemblyProviderType(applicationWebSiteName);
            var configuration = new TestConfigurationProvider();
            configuration.Configuration.Set(
                typeof(IAssemblyProvider).FullName,
                providerType.AssemblyQualifiedName);
            services.AddInstance<ITestConfigurationProvider>(configuration);
            services.AddInstance<ILoggerFactory>(new LoggerFactory());


            if (configureServices != null)
            {
                configureServices(services);
            }
        }