如何对剃须刀视图进行单元测试 Request.IsAuthenticated
How to unit test Request.IsAuthenticated of razor view
我正在使用 RazorGenerator 来测试视图。但是当我尝试模拟请求时,我可以看到 IsAuthenticated 属性 被填充但是呈现时的视图没有 return 所需的 HTML.
_Login.cshtml
@using System.Security.Claims
@if (Request.IsAuthenticated)
{
<text>
<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink(@welcome, null, null, null, new { id = "test1" })
</li>
<li>
@Html.ActionLink("Sign out", "SignOut", "Account")
</li>
</ul>
</text>
}else
{
<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink("Log in", "SignIn", "Account", null, new {id = "test2" })
</li>
</ul>
}
I have the following test case which always gives me the html from else condition.
public void test23()
{
v_Request.SetupGet(x => x.IsAuthenticated).Returns(true);
v_Context.SetupGet(x => x.Request).Returns(v_Request.Object);
v_ControllerContext = new Mock<ControllerContext>(
v_Context.Object,
new RouteData(),
v_Controller.Object);
v_ControllerContext.Setup(x => x.HttpContext).Returns(v_Context.Object);
v_ViewContext = new Mock<ViewContext>(
v_ControllerContext.Object,
new Mock<IView>().Object,
null,
new TempDataDictionary(),
null);
_Views_Shared__Login_cshtml view = new _Views_Shared__Login_cshtml();
view.ViewContext = new ViewContext(v_ControllerContext.Object, new Mock<IView>().Object, view.ViewData, new TempDataDictionary(), new Mock<TextWriter>().Object);
HtmlDocument doc = view.RenderAsHtml();
}
_Views_Shared__Login_cshtml 是由 RazorGenerator 生成的 class。有线索吗..?
如果您想对视图进行单元测试,您可以将其视为要求重构代码的提示。
视图很少被测试,因为它们不应该有太多的功能。测试应该集中在生成它的代码上。
如您所说,视图来自父视图。
您可以创建一个过滤器:
public class MyAuthFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
ViewBag=filterContext.HttpContext.Request.IsAuthenticated;
}
}
注册它:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new MyAuthFilterAttribute ());
}
并且您将始终填充 ViewBag
我正在使用 RazorGenerator 来测试视图。但是当我尝试模拟请求时,我可以看到 IsAuthenticated 属性 被填充但是呈现时的视图没有 return 所需的 HTML.
_Login.cshtml
@using System.Security.Claims
@if (Request.IsAuthenticated)
{
<text>
<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink(@welcome, null, null, null, new { id = "test1" })
</li>
<li>
@Html.ActionLink("Sign out", "SignOut", "Account")
</li>
</ul>
</text>
}else
{
<ul class="nav navbar-nav navbar-right">
<li>
@Html.ActionLink("Log in", "SignIn", "Account", null, new {id = "test2" })
</li>
</ul>
}
I have the following test case which always gives me the html from else condition.
public void test23()
{
v_Request.SetupGet(x => x.IsAuthenticated).Returns(true);
v_Context.SetupGet(x => x.Request).Returns(v_Request.Object);
v_ControllerContext = new Mock<ControllerContext>(
v_Context.Object,
new RouteData(),
v_Controller.Object);
v_ControllerContext.Setup(x => x.HttpContext).Returns(v_Context.Object);
v_ViewContext = new Mock<ViewContext>(
v_ControllerContext.Object,
new Mock<IView>().Object,
null,
new TempDataDictionary(),
null);
_Views_Shared__Login_cshtml view = new _Views_Shared__Login_cshtml();
view.ViewContext = new ViewContext(v_ControllerContext.Object, new Mock<IView>().Object, view.ViewData, new TempDataDictionary(), new Mock<TextWriter>().Object);
HtmlDocument doc = view.RenderAsHtml();
}
_Views_Shared__Login_cshtml 是由 RazorGenerator 生成的 class。有线索吗..?
如果您想对视图进行单元测试,您可以将其视为要求重构代码的提示。
视图很少被测试,因为它们不应该有太多的功能。测试应该集中在生成它的代码上。
如您所说,视图来自父视图。 您可以创建一个过滤器:
public class MyAuthFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
ViewBag=filterContext.HttpContext.Request.IsAuthenticated;
}
}
注册它:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new MyAuthFilterAttribute ());
}
并且您将始终填充 ViewBag