Mocking RouteTable.Routes.GetVirtualPath in MVC

I have a paging controller with a method that calls

RouteTable.Routes.GetVirtualPath(this.viewContext.RequestContext, valueDictionary)

I'm trying to test this method with Rhino's new products, and I'm not sure how to mock GetVirtualPath to return a non-zero route. I'm mocking RequestContext, but I'm not sure which methods / properties need to be trimmed.

Shortening mockRequestContext, it follows that GetVirtualPath returns a nonzero path, and if so, then what needs to be trimmed for this?

Any advice is greatly appreciated.

+3
source share
2 answers

Not sure if I understand your question, but it looks something like this:

IRouteTable routeTableMock = RhinoMocks.CreateMock<IRouteTable>();

// assuming that IRouteTable.Routes is of type IRoutes.
IRoutes routes = RhinoMocks.CreateMock<IRoutes>();

// return the routes with the mock
routeTableMock.Stub(x => x.Routes).Return(routes);

// setup the mock for GetVirtualPath
routes.Stub(
  x => x.GetVirtualPath(
    Arg<RequestContext>.Is.Anything, 
    Arg<IDictionary>.Is.Anything)
  .Return(/* whatever */);

, , , .

+1

, URL-, :

new UrlHelper(ControllerContext.RequestContext).RouteUrl(routeValueDictionary)

... ControllerContext:

var mockHttpContext = new Mock<HttpContextBase>();

var controllerContext
    = new ControllerContext(
        mockHttpContext.Object,
        new RouteData(), 
        new Mock<ControllerBase>().Object);

var myController = new MyController { ControllerContext = controllerContext };

, , , .

+1

Source: https://habr.com/ru/post/1707177/


All Articles