在ASP.NET MVC里我们可以很方便的对模型Model、控制器Controller进行单元测试,但对于
视图View的测试就没那么容易和直接了。在这里介绍一种方法,使用RazorGenerator对View进行
单元测试。
废话不多说,直接上图。主要方法和步骤如下:
1、创建ASP.NET MVC 项目和相应的测试项目。
2、在Web项目中nuget添加包:RazorGenerator.MsBuild。
3、在测试项目中nuget添加包:RazorGenerator.Testing;添加包 NUnit
4、在Web项目中修改待测试的代码 Views\Shared\Error.cshtml
@model System.Web.Mvc.HandleErrorInfo @{ ViewBag.Title = "Error"; } <div id="content"> <h1 class="text-danger">Error.</h1> <h2 class="text-danger">An error occurred while processing your request.</h2> </div> |