如何用最小起订量模拟控制器RouteData?

发布于 2024-10-30 04:21:31 字数 290 浏览 0 评论 0原文

我的视图有一个文件附件以及验证码验证。问题是我使用 MVCMockHelpers 进行文件验证,但它重新初始化路线数据(新RouteData()),同时设置控制器上下文。我的验证码验证总是失败,因为它试图从 RouteData 获取值。非常感谢有关如何模拟路线数据的任何帮助。

提前致谢。

问候,

拉贾

My view has a file attachment as well as Captcha validation. The problem is I am using MVCMockHelpers for my file validation but it reinitializes the route data (new RouteData()) while setting the controller context. My captcha validation always fails since it is trying to get the values from RouteData. Any help on how to mock route data is much appreciated.

Thanks in Advance.

Regards,

Raja

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方觉久 2024-11-06 04:21:31
var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);

var mockedRequest = new Mock<HttpRequestBase>();
mockedRequest.Setup(m => m.AppRelativeCurrentExecutionFilePath).Returns("~/");

var mockedHttpContext = new Mock<HttpContextBase>();
mockedHttpContext.Setup(m => m.Request).Returns(mockedRequest.Object);

var controllerContext = new Mock<ControllerContext>();
controllerContext.Setup(c => c.RouteData).Returns(routes.GetRouteData(mockedHttpContext.Object));
controllerContext.Setup(c => c.HttpContext).Returns(mockedHttpContext.Object);
controllerContext.Setup(c => c.Controller).Returns(new Mock<ControllerBase>().Object);

我有类似的东西 - 我倾向于将所有这些类型的模拟包装在一个助手中,并在必要时修改测试中的设置,例如更改 AppRelativeCurrentExecutionFilePath 或路由数据。

var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);

var mockedRequest = new Mock<HttpRequestBase>();
mockedRequest.Setup(m => m.AppRelativeCurrentExecutionFilePath).Returns("~/");

var mockedHttpContext = new Mock<HttpContextBase>();
mockedHttpContext.Setup(m => m.Request).Returns(mockedRequest.Object);

var controllerContext = new Mock<ControllerContext>();
controllerContext.Setup(c => c.RouteData).Returns(routes.GetRouteData(mockedHttpContext.Object));
controllerContext.Setup(c => c.HttpContext).Returns(mockedHttpContext.Object);
controllerContext.Setup(c => c.Controller).Returns(new Mock<ControllerBase>().Object);

I have something akin to this - I tend to wrap all these kinds of Mocks up in a helper and if necessary modify the setup from the test, such as changing the AppRelativeCurrentExecutionFilePath or the route data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文