如何使用RenderPartialViewToString为返回JsonResult的方法编写单元测试? [英] How To Write Unit Test For Method Returning JsonResult With RenderPartialViewToString?
问题描述
如果您查看此链接上的示例:
If you look at the example at this link:
http://www. atlanticbt.com/blog/asp-net-mvc-using-ajax-json-and-partialviews/
一个人如何为JsonAdd方法编写单元测试?我自己的代码中也有类似的情况,但是调用时出现RenderPartialViewToString错误:
How would one write a unit test for the JsonAdd method? I have a similar situation in my own code, but the RenderPartialViewToString errors when calling:
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView
我尝试了各种方法来尝试对无效的调用进行存根.任何帮助表示赞赏.谢谢.
I've tried different ways of trying to stub that call to no avail. Any help appreciated. Thanks.
推荐答案
由于ViewEninges是静态类,因此您无法使用RhinoMocks对其进行模拟.我认为您最好的选择是创建一个局部视图渲染器"界面.接口是可模拟的,因此您可以消除渲染视图的复杂性.这是一些快速的伪代码.
Since ViewEninges is a static class, you can't mock it with RhinoMocks. I think your best bet is to create a "partial view renderer" interface. An interface is mockable so you'll be able to stub out the complexity of rendering the view. Here's some quick pseudo-code thrown together.
首先,定义部分视图渲染器接口:
First, define the partial view renderer interface:
public interface IRenderPartialView
{
string Render(string viewName, object model);
}
然后,将基类的RenderPartialViewToString更改为IRenderPartialView.Render的实现:
Then, change your base class' RenderPartialViewToString to be the implementation of IRenderPartialView.Render:
public abstract class BaseController : Controller, IRenderPartialView
{
...
public string Render(string viewName, object model)
{
// same code as RenderPartialViewToString
}
}
现在,我们需要更改您的控制器构造函数,以便我们可以在测试期间注入IRenderPartialView,但是在生产过程中使用基类.我们可以通过使用一对构造函数来完成此任务:
Now we need to change your controller constructors so we can inject an IRenderPartialView during testing -- but use the base class one during production. We can accomplish this by using a pair of constructors:
public class YourController : BaseController
{
private IRenderPartialView partialRenderer;
public YourController()
{
SetRenderer(this);
}
public YourController(IRenderPartialView partialRenderer)
{
SetRenderer(partialRenderer);
}
private void SetRenderer(IRenderPartialView partialRenderer)
{
this.partialRenderer = this;
}
}
现在,JsonAdd可以调用部分视图渲染器了:
Now, JsonAdd can call the partial view renderer:
public JsonResult JsonAdd(AddPersonViewModel AddPersonModel)
{
...
return Json(new
{
Success = true,
Message = "The person has been added!",
PartialViewHtml = partialRenderer.Render("PersonList", new PersonListViewModel {PersonList = _personList})
});
}
因此,在测试期间,您将模拟出IRenderPartialView
并将其发送到接受IRenderPartialView
的构造函数.在生产过程中,当ASP.NET MVC调用默认构造函数时,它将使用控制器作为渲染器(在基类中具有IRenderPartialView.Render
的实现).
So, during testing, you'll mock out an IRenderPartialView
and send that to the constructor that accepts an IRenderPartialView
. During production, when ASP.NET MVC calls your default constructor, it will use the controller as the renderer (which has the implementation of IRenderPartialView.Render
inside the base class).
这篇关于如何使用RenderPartialViewToString为返回JsonResult的方法编写单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!