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); } }
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); } }