ASP.NET MVC:使用 UrlHelper 的单元测试控制器

编程入门 行业动态 更新时间:2024-10-23 11:24:31
本文介绍了ASP.NET MVC:使用 UrlHelper 的单元测试控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的控制器操作之一,在 Ajax 请求中被调用,正在向客户端返回一个 URL,以便它可以进行重定向.我正在使用 Url.RouteUrl(..) 并且在我的单元测试期间这失败了,因为 Controller.Url 参数没有预填充.

One of my controllers actions, one that is being called in an Ajax request, is returning an URL to the client side so it can do a redirection. I'm using Url.RouteUrl(..) and during my unit tests this fails since the Controller.Url parameter is not pre-filled.

我尝试了很多方法,其中包括尝试存根 UrlHelper(失败),手动创建带有 RequestContext 的 UrlHelper有一个存根 HttpContextBase(在 RouteCollection.GetUrlWithApplicationPath 调用中失败).

I tried a lot of things, among others attempting to stub UrlHelper (which failed), manually creating a UrlHelper with a RequestContext that has a stubbed HttpContextBase (which failed on a RouteCollection.GetUrlWithApplicationPath call).

我已经在 Google 上搜索过,但几乎没有找到关于该主题的任何内容.我是否在控制器操作中使用 Url.RouteUrl 做了一些非常愚蠢的事情?有没有更简单的方法?

I have searched Google but found virtually nothing on the subject. Am I doing something incredibly stupid using Url.RouteUrl in my Controller action? Is there an easier way?

更糟糕的是,我希望能够在我的单元测试中测试返回的 URL - 事实上我只对知道它重定向到正确的路由感兴趣,但是因为我返回的是一个 URL而不是路由,我想控制解析的 URL(例如,通过使用存根 RouteCollection) - 但我很高兴让我的测试通过.

To make it even worse, I'd like to be able to test the returned URL in my unit test - in fact I'm only interested in knowing it's redirecting to the right route, but since I'm returning an URL instead of a route, I would like to control the URL that is resolved (eg. by using a stubbed RouteCollection) - but I'll be happy to get my test passing to begin with.

推荐答案

这是我的一个测试(xUnit + Moq),仅用于类似情况(在控制器中使用 Url.RouteUrl)

Here is one of my tests (xUnit + Moq) just for similar case (using Url.RouteUrl in controller)

希望这有帮助:

var routes = new RouteCollection(); MvcApplication.RegisterRoutes(routes); var request = new Mock<HttpRequestBase>(MockBehavior.Strict); request.SetupGet(x => x.ApplicationPath).Returns("/"); request.SetupGet(x => x.Url).Returns(new Uri("localhost/a", UriKind.Absolute)); request.SetupGet(x => x.ServerVariables).Returns(new System.Collections.Specialized.NameValueCollection()); var response = new Mock<HttpResponseBase>(MockBehavior.Strict); response.Setup(x => x.ApplyAppPathModifier("/post1")).Returns("localhost/post1"); var context = new Mock<HttpContextBase>(MockBehavior.Strict); context.SetupGet(x => x.Request).Returns(request.Object); context.SetupGet(x => x.Response).Returns(response.Object); var controller = new LinkbackController(dbF.Object); controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller); controller.Url = new UrlHelper(new RequestContext(context.Object, new RouteData()), routes);

更多推荐

ASP.NET MVC:使用 UrlHelper 的单元测试控制器

本文发布于:2023-11-14 13:47:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587381.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   单元测试   NET   ASP   UrlHelper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!