如何为使用 RenderPartialViewToString 返回 JsonResult 的方法编写单元测试?
如果您查看此链接中的示例:
http://www.atlanticbt.com/blog/asp-net-mvc-using-ajax-json-and-partialviews/
如何为 JsonAdd 方法编写单元测试?我自己的代码中也有类似的情况,但是调用时出现 RenderPartialViewToString 错误:
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView
我尝试了不同的方法来尝试存根该调用,但无济于事。任何帮助表示赞赏。谢谢。
If you look at the example at this link:
http://www.atlanticbt.com/blog/asp-net-mvc-using-ajax-json-and-partialviews/
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
I've tried different ways of trying to stub that call to no avail. Any help appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在使用 RenderPartialViewToString 进行单元测试时遇到了很多麻烦。
我通过做两件事成功了。
我必须模拟视图引擎和控制器上下文。
这是代码:
这就是我使用它的方式:
这是我的来源:
查看引擎模拟:http://thoai- nguyen.blogspot.fr/2011/04/test-mock-mvc-view-engine.html
控制器上下文模拟:ASP.NET MVC - 使用 Moq 框架对 RenderPartialViewToString() 进行单元测试?
希望有所帮助。
I had a lot of trouble to make unit test working with RenderPartialViewToString.
I succeeded by doing 2 things.
I had to mock the view engine and the controller context.
Here the code :
And that is the way i use it all :
Here are my sources :
View engine mocking : http://thoai-nguyen.blogspot.fr/2011/04/test-mock-mvc-view-engine.html
Controller context mocking : ASP.NET MVC - Unit testing RenderPartialViewToString() with Moq framework?
Hope this help.
由于 ViewEninges 是一个静态类,因此您无法使用 RhinoMocks 来模拟它。我认为你最好的选择是创建一个“部分视图渲染器”界面。界面是可模拟的,因此您将能够消除渲染视图的复杂性。这是一些快速的伪代码。
首先,定义部分视图渲染器接口:
然后,将基类的 RenderPartialViewToString 更改为 IRenderPartialView.Render 的实现:
现在我们需要更改控制器构造函数,以便我们可以在测试期间注入 IRenderPartialView - 但使用基类一在生产过程中。我们可以通过使用一对构造函数来完成此操作:
现在,JsonAdd 可以调用部分视图渲染器:
因此,在测试期间,您将模拟一个
IRenderPartialView
并将其发送到接受IRenderPartialView
的构造函数代码>IRenderPartialView。在生产过程中,当 ASP.NET MVC 调用默认构造函数时,它将使用控制器作为渲染器(在基类中具有 IRenderPartialView.Render 的实现)。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:
Then, change your base class' RenderPartialViewToString to be the implementation of IRenderPartialView.Render:
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:
Now, JsonAdd can call the partial view renderer:
So, during testing, you'll mock out an
IRenderPartialView
and send that to the constructor that accepts anIRenderPartialView
. During production, when ASP.NET MVC calls your default constructor, it will use the controller as the renderer (which has the implementation ofIRenderPartialView.Render
inside the base class).