/// <summary> /// Helper method which creates an ApplicationContext with very little setup. /// </summary> /// <returns>An ApplicationContext instance setup for unit testing.</returns> /// <remarks> /// <para>This should be the equivalent of BaseWebTest.Initialize() in the Umbraco codebase.</para> /// <code> /// ApplicationContext = new ApplicationContext() { IsReady = true }; /// </code> /// </remarks> private static global::Umbraco.Core.ApplicationContext CreateApplicationContext() { Type applicationContextType = typeof(global::Umbraco.Core.ApplicationContext); BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; ConstructorInfo constructor = applicationContextType.GetConstructor(bindingFlags, null, CallingConventions.Any, Type.EmptyTypes, null); PropertyInfo propertyInfo = applicationContextType.GetProperty("IsReady", bindingFlags); global::Umbraco.Core.ApplicationContext context = (global::Umbraco.Core.ApplicationContext)constructor.Invoke(null); propertyInfo.SetValue(context, true, null); return(context); }
/// <summary> /// Helper method which sets the UmbracoContext property of the /// </summary> /// <param name="httpContext">The <see cref="HttpContextBase"/> instance which the <see cref="UmbracoContext"/> instance wraps. The URL property must be set.</param> /// <returns>An <see cref="UmbracoContext"/> instance which can be used for basic unit testing of surface controllers.</returns> public static UmbracoContext CreateContext(HttpContextBase httpContext) { Type umbracoContextType = typeof(UmbracoContext); Assembly umbracoWebAssembly = umbracoContextType.Assembly; global::Umbraco.Core.ApplicationContext applicationContext = CreateApplicationContext(); BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; Type[] umbracoContextConstructorArgTypes = new Type[] { typeof(HttpContextBase), typeof(global::Umbraco.Core.ApplicationContext), umbracoWebAssembly.GetType("Umbraco.Web.Routing.IRoutesCache") }; ConstructorInfo umbracoContextConstructor = umbracoContextType.GetConstructor(bindingFlags, null, CallingConventions.Any, umbracoContextConstructorArgTypes, null); UmbracoContext umbracoContext = (UmbracoContext)umbracoContextConstructor.Invoke(new object[] { httpContext, applicationContext, null }); SetRoutingContext(umbracoContext); return(umbracoContext); }