I'm trying to figure out a way to inspect a razor view's rendered HTML within a test.
I've been looking at posts where people have asked similar questions, but each time, I fall short. The problem I'm getting is when the view engine tries to load the .cshtml file. It's returning a null reference exception from within the function.
[Test]
public void GetRazorViewEngine() {
var _HttpContextBaseMock = new Mock<HttpContextBase>();
var _HttpRequestMock = new Mock<HttpRequestBase>();
var _HttpResponseMock = new Mock<HttpResponseBase>();
_HttpContextBaseMock.SetupGet(x => x.Request).Returns(_HttpRequestMock.Object);
_HttpContextBaseMock.SetupGet(x => x.Response).Returns(_HttpResponseMock.Object);
var routeData = new RouteData();
routeData.Values.Add("controller", "Home");
routeData.Values.Add("action", "About");
var controller = new HomeController();
controller.ControllerContext = new
ControllerContext(_HttpContextBaseMock.Object,
routeData,
controller);
controller.Url = new UrlHelper(new RequestContext(_HttpContextBaseMock.Object, routeData),
new RouteCollection());
var razorEngine = ViewEngines.Engines
.Where(x => x.GetType() == typeof(System.Web.Mvc.RazorViewEngine))
.FirstOrDefault();
var path = "/Users/dan/Projects/Playground/MvcPlayground/Views/Home/About.cshtml";
var master = "/Users/dan/Projects/Playground/MvcPlayground/Views/Shared/_Layout.cshtml";
ViewEngineResult viewEngineResult = razorEngine.FindView(controller.ControllerContext,
path,
master,
false);
}
Here is the stack trace of the error.
at System.Web.WebPages.FileExistenceCache.<.ctor>b__4 (System.String path) <0x3880c90 + 0x00022> in :0 at System.Collections.Concurrent.ConcurrentDictionary
2[TKey,TValue].GetOrAdd (System.Collections.Concurrent.TKey key, System.Func
2 valueFactory) [0x00037] in /private/tmp/source-mono-4.4.0-c7sr0/bockbuild-mono-4.4.0-branch-c7sr0/profiles/mono-mac-xamarin/build-root/mono-x86/external/referencesource/mscorlib/system/collections/Concurrent/ConcurrentDictionary.cs:1049 at System.Web.WebPages.FileExistenceCache.FileExists (System.String virtualPath) <0x3880880 + 0x0003f> in :0 at System.Web.Mvc.BuildManagerViewEngine.FileExists (System.Web.Mvc.ControllerContext controllerContext, System.String virtualPath) <0x38807f8 + 0x0001f> in :0 at System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromSpecificName (System.Web.Mvc.ControllerContext controllerContext, System.String name, System.String cacheKey, System.String[]& searchedLocations) <0x369b9f8 + 0x00039> in :0 at System.Web.Mvc.VirtualPathProviderViewEngine.GetPath (System.Web.Mvc.ControllerContext controllerContext, System.String[] locations, System.String[] areaLocations, System.String locationsPropertyName, System.String name, System.String controllerName, System.String cacheKeyPrefix, Boolean useCache, System.String[]& searchedLocations) <0x369af00 + 0x0033b> in :0 at System.Web.Mvc.VirtualPathProviderViewEngine.FindView (System.Web.Mvc.ControllerContext controllerContext, System.String viewName, System.String masterName, Boolean useCache) <0x369aa30 + 0x000b7> in :0 at MvcPlayground.Tests.HomeControllerTest.GetRazorViewEngine () [0x00163] in /Users/dan/Projects/Playground/MvcPlayground.Tests/Controllers/HomeControllerTest.cs:82 at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in /private/tmp/source-mono-4.4.0-c7sr0/bockbuild-mono-4.4.0-branch-c7sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/corlib/System.Reflection/MonoMethod.cs:295
Look at (Razor engine cant find view) also note that RazorViewEngine's had some problems with pre-compiled views in the past: https://github.com/aspnet/Mvc/issues/6672. However, It looks to me like RazorViewEngine has changed a little bit within the past 5 years. I'd say it is time to update and try again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With