视觉工作室隐藏了aspx页面中的拼写错误(而不是后面的代码)真的很烦人.如果编译器会编译它们,我会收到编译错误.
我正在开发一个ASP.NET MVC 4 Web应用程序,它可以生成大而复杂的报告.我想编写单元测试来渲染视图,以确保视图不会爆炸,具体取决于模型:
[Test]
public void ExampleTest(){
var reportModel = new ReportModel();
var reportHtml = RenderRazorView(
@"..\..\Report.Mvc\Views\Report\Index.cshtml",
reportModel);
Assert.IsFalse(
string.IsNullOrEmpty(reportHtml),
"View Failed to Render!");
}
public string RenderRazorView(string viewPath, object model){
//WHAT GOES HERE?
}
Run Code Online (Sandbox Code Playgroud)
我已经在网络上看到了很多有关这方面的信息,但它要么反对测试竞争,要么只能在Web请求的上下文中使用.
HttpContextBase)来调用.我一直在努力调整渲染视图作为字符串来处理Mocked HttpContextBase,但在使用Mocked时遇到了问题ControllerContext:
你调用的对象是空的.在System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext,String [] locations,String [] areaLocations的System.Web.Mvc.ControllerContext.get_DisplayMode()上的System.Web.WebPages.DisplayModeProvider.GetDisplayMode(HttpContextBase context)中, String locationsPropertyName,String name,String controllerName,String cacheKeyPrefix,Boolean useCache,String []&searchingLocations)
这是我到目前为止的代码:
public string RenderRazorView(string viewPath, object model)
{ …Run Code Online (Sandbox Code Playgroud) 嗨,您好
我想知道是否有更好的方法来测试视图已在MVC中呈现。
我在想也许应该将视图呈现为字符串,但是也许有更简单的方法?
基本上,我想知道给定操作的视图是否已正确呈现,我已经在测试视图模型,但是我想看到呈现正确ViewData.Model的视图可以正常工作