Dan*_*gne 11 c# asp.net-mvc unit-testing razor xamarin
我正试图找出一种方法来检查剃刀视图在测试中呈现的HTML.
我一直在看人们提出类似问题的帖子,但每一次,我都做不到.我得到的问题是视图引擎尝试加载.cshtml文件.它从函数中返回一个空引用异常.
[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);
}
Run Code Online (Sandbox Code Playgroud)
这是错误的堆栈跟踪.
在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.Func2 valueFactory)[0x00037] in/private/tmp/source-单4.4.0-c7sr0/bockbuild -单4.4.0分支-c7sr0 /简档/单MAC-xamarin /建立根/单声道86 /外部/ referencesource/mscorlib程序/系统/收集/并行/ 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,System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromSpecificName(System.Web.Mvc.ControllerContext controllerContext,System.String name,System.String cacheKey,System.String []&searchingLocations)<0x369b9f8 + 0x00039> in:0,System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(System.Web.Mvc.ControllerContext controllerCo)ntext,System.String [] locations,System.String [] areaLocations,System.String locationsPropertyName,System.String name,System.String controllerName,System.String cacheKeyPrefix,Boolean useCache,System.String []&searchingLocations)<0x369af00 + 0x0033b> in:0在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]在/Users/dan/Projects/Playground/MvcPlayground.Tests/Controllers/HomeControllerTest.cs:82 at(包装器托管到本机)System.Reflection.MonoMethod:InternalInvoke(System.Reflection) System.Reflection.MonoMethod.Invoke中的.MonoMethod,object,object [],System.Exception&)(System.Object obj,BindingFlags invokeAttr,System.Reflection.Binder binder,System.Object []参数,System.Globalization.CultureInfo文化)在/ private/tmp/source-mono-中的[0x00038] 4.4.0-c7sr0/bockbuild -单4.4.0分支-c7sr0 /简档/单MAC-xamarin /建立根/单声道86/MCS /类/ corlib /的System.Reflection/MonoMethod.cs:295
小智 1
查看(Razor引擎找不到视图)还请注意,RazorViewEngine过去在预编译视图方面存在一些问题: https: //github.com/aspnet/Mvc/issues/6672。然而,在我看来,RazorViewEngine 在过去 5 年内发生了一些变化。我想说是时候更新并重试了。
| 归档时间: |
|
| 查看次数: |
730 次 |
| 最近记录: |