我有一个视图,它内部有部分视图渲染:
<div class="partialViewDiv">
@Html.RenderPartial("partial", Model.SomeModelProperty);
</div>
和一个返回此视图的控制器
public ActionResult Action()
{
...
var model = new SomeModel(){SomeModelProperty = "SomeValue"}
return View("view", model);
}
我知道如何测试视图的渲染:
[TestMethod]
public void TestView()
{
...
var result = controller.Action();
// Assert
result.AssertViewRendered().ForView("view").WithViewData<SomeModel>();
}
但当我打电话时
result.AssertPartialViewRendered().ForView("partial").WithViewData<SomeModelPropertyType>();
我收到此错误消息
Expected result to be of type PartialViewResult. It is actually of type ViewResult.
我究竟做错了什么?
我究竟做错了什么?
您正在测试控制器:此类测试本质上是模拟视图,只是验证控制器是否返回预期的视图(和模型)。
因为渲染 PartialView“部分”的 View“视图”不参与测试,所以你无法测试它是否按照你的预期进行。
一般来说,大多数人不会对视图进行单元测试;但如果你想这样做看看这个博客 http://blog.davidebbo.com/2011/06/unit-test-your-mvc-views-using-razor.html或谷歌搜索“MVC 单元测试视图”
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)