gpt4 book ai didi

unit-testing - 如何为使用 RenderPartialViewToString 返回 JsonResult 的方法编写单元测试?

转载 作者:行者123 更新时间:2023-12-04 04:19:10 27 4
gpt4 key购买 nike

如果您查看此链接中的示例:

http://www.atlanticbt.com/blog/asp-net-mvc-using-ajax-json-and-partialviews/

如何为 JsonAdd 方法编写单元测试?我自己的代码也有类似的情况,但是调用时出现 RenderPartialViewToString 错误:

ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView

我尝试了不同的方法来尝试 stub 该调用,但均无济于事。任何帮助表示赞赏。谢谢。

最佳答案

我在使用 RenderPartialViewToString 进行单元测试时遇到了很多麻烦。我成功地做了两件事。我不得不模拟 View 引擎和 Controller 上下文。

这里是代码:

public ViewEngineResult SetupViewContent(string viewName, string viewHtmlContent)
{
var mockedViewEngine = new Mock<IViewEngine>();
var resultView = new Mock<IView>();

resultView.Setup(x => x.Render(It.IsAny<ViewContext>(), It.IsAny<TextWriter>()))
.Callback<ViewContext, TextWriter>((v, t) =>
{
t.Write(viewHtmlContent);
});

var viewEngineResult = new ViewEngineResult(resultView.Object, mockedViewEngine.Object);
mockedViewEngine.Setup(x => x.FindPartialView(It.IsAny<ControllerContext>(), viewName, It.IsAny<bool>()))
.Returns<ControllerContext, string, bool>((controller, view, useCache) =>
{
return viewEngineResult;
});

mockedViewEngine.Setup(x => x.FindView(It.IsAny<ControllerContext>(), viewName, It.IsAny<string>(), It.IsAny<bool>()))
.Returns<ControllerContext, string, string, bool>((controller, view, masterName, useCache) =>
{
return viewEngineResult;
});

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(mockedViewEngine.Object);
return viewEngineResult;
}

public void SetContext(ref PointCollecteLivraisonController controller)
{
SetupViewContent("MyViewName", "TheViewContent");

var httpContextBase = new Mock<HttpContextBase>();
var httpRequestBase = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var routes = new RouteCollection();
RouteConfigurator.RegisterRoutes(routes);
var routeData = new RouteData();
routeData.Values.Add("controller", "PointCollecteLivraison");
routeData.Values.Add("action", "RechercheJson");

httpContextBase.Setup(x => x.Response).Returns(response.Object);
httpContextBase.Setup(x => x.Request).Returns(httpRequestBase.Object);
httpContextBase.Setup(x => x.Session).Returns(session.Object);
session.Setup(x => x["somesessionkey"]).Returns("value");
httpRequestBase.Setup(x => x.Form).Returns(new NameValueCollection());
controller.ControllerContext = new ControllerContext(httpContextBase.Object, routeData, controller);
controller.Url = new UrlHelper(new RequestContext(controller.HttpContext, routeData), routes);
}

这就是我使用它的方式:

PointCollecteLivraisonController controller = new PointCollecteLivraisonController();
SetContext(ref controller);

这是我的来源:查看引擎模拟:http://thoai-nguyen.blogspot.fr/2011/04/test-mock-mvc-view-engine.html

Controller 上下文模拟:ASP.NET MVC - Unit testing RenderPartialViewToString() with Moq framework?

希望对您有所帮助。

关于unit-testing - 如何为使用 RenderPartialViewToString 返回 JsonResult 的方法编写单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3462006/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com