Example #1
0
        private Func <IServiceCollection, IServiceProvider> InitializeServices(
            Assembly startupAssembly,
            Func <IServiceCollection, IServiceProvider> buildServices)
        {
            // When an application executes in a regular context, the application base path points to the root
            // directory where the application is located, for example .../samples/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, we need to calculate the correct project path and override the application
            // environment value so that components like the view engine work properly in the context of the test.
            return((services) =>
            {
#if DNX451
                AppDomain.CurrentDomain.SetData("APP_CONTEXT_BASE_DIRECTORY", ApplicationRoot.GetDirectoryName(startupAssembly));
#endif
                services.AddInstance <IApplicationEnvironment>(new TestApplicationEnvironment(startupAssembly));

                services.AddInstance <IHostingEnvironment>(new TestHostingEnvironment(startupAssembly));

                // Inject a custom assembly provider. Overrides AddMvc() because that uses TryAdd().
                //services.AddInstance<IAssemblyProvider>(new TestStaticAssemblyProvider(startupAssembly));

                AddAdditionalServices(services);

                return buildServices(services);
            });
        }
Example #2
0
 public TestApplicationEnvironment(Assembly startupAssembly) : this(
         PlatformServices.Default.Application,
         startupAssembly.GetName().Name,
         ApplicationRoot.GetDirectoryName(startupAssembly))
 {
 }
Example #3
0
 public TestHostingEnvironment(Assembly assembly) : this(ApplicationRoot.GetDirectoryName(assembly))
 {
 }