开发者

How to mock Controller RouteData with MOQ?

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 Adv开发者_运维问答ance.

Regards,

Raja


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜