MVC Razor View Render in test

I am trying to figure out a way to check the appearance of the razor displayed by the HTML in the test.

I watched posts where people asked similar questions, but every time I don’t understand. The problem I get is when the view engine tries to load the .cshtml file. It returns a null reference exception from a 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 error stack trace.

in 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 in System.Web.WebPages.FileExistenceCache.FileExists (System.String virtualPath) <0x3880880 + 0x0003f> in: 0 at System.Web.Mvc.BuildManagerViewEngine.ile System.Web.Mvc.ControllerContext controllerContext, System.String virtualPath) <0x38807f8 + 0x0001f> in: 0 at System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromSpecificName (System.Web.Mvc.ControllerContext.Context.Context.Contentler cacheKey, System.String [] & searchedLocations) <0x369b9f8 + 0x00039> in: 0 at System.Web.Mvc.VirtualPathProviderViewEngine.GetPa th (System.Web.Mvc.ControllerContext controllerContext, System.String [] locations, System.String [] areaLocations, System.String locationPropertyName, name System.String, System.String controllerName, System.String cacheKeyPrefix, Boolean useCache, System. String [] & searchLocations) <0x369af00 + 0x0033b> in: 0 in System.Web.Mvc.VirtualPathProviderViewEngine.FindView (System.Web.Mvc.ControllerContext controllerContext, System.String viewName, System.String masterName, Boolean useCachea <0x0) 0x000b7> at: 0 on MvcPlayground.Tests.HomeControllerTest.GetRazorViewEngine () [0x00163] in /Users/dan/Projects/Playground/MvcPlayground.Tests/Controllers/HomeControllerTest.cs:82 at (based system-driven wrapper) .MonoMethod: InternalInvoke (System.Reflection.MonoMethod, object, object [], System.Exception &) in 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

+5
source share

All Articles