Not able to retrieve RouteData in a MVC project

564 views Asked by At

I am using MVC5 to create a project on culture basis. I am creating some test cases for my App using MOQ, but I am not able to find my Route in this. I am using a class named as MockHttpContext to get my httpcontext like below.

public static HttpContextBase MockHttpContext()
{                    
   var context = new Mock<HttpContextBase>();
   var request = new Mock<HttpRequestBase>();
   var response = new Mock<HttpResponseBase>();
   var session = new Mock<HttpSessionStateBase>();
   var server = new Mock<HttpServerUtilityBase>();
   var user = new Mock<IPrincipal>();
   var identity = new Mock<IIdentity>();
   var urlHelper = new Mock<UrlHelper>();

   var routes = new RouteCollection();
   RouteTable.Routes.Clear();
   var areaRegistration = new AdminAreaRegistration();
   var areaRegistrationContext = new AreaRegistrationContext(
   areaRegistration.AreaName, RouteTable.Routes);
   areaRegistration.RegisterArea(areaRegistrationContext);           

   RouteConfig.RegisterRoutes(RouteTable.Routes);
   var requestContext = new Mock<RequestContext>();
   requestContext.Setup(x => x.HttpContext).Returns(context.Object);
   context.Setup(ctx => ctx.Request).Returns(request.Object);
   context.Setup(ctx => ctx.Response).Returns(response.Object);
   context.Setup(ctx => ctx.Session).Returns(session.Object);
   context.Setup(ctx => ctx.Server).Returns(server.Object);
   context.Setup(ctx => ctx.User).Returns(user.Object);
   user.Setup(ctx => ctx.Identity).Returns(identity.Object);
   identity.Setup(id => id.IsAuthenticated).Returns(true);
   identity.Setup(id => id.Name).Returns("test");
   request.Setup(req => req.Url).Returns(new Uri("http://localhost/"));
   request.Setup(req => req.RequestContext).Returns(requestContext.Object);
   requestContext.Setup(x => x.RouteData).Returns(new RouteData());
   request.SetupGet(req => req.Headers).Returns(new NameValueCollection());
   return context.Object;
 }

   //this method is being called in my test class like..

[TestMethod]
public void Index()
{
   try
   {
      var controller = new HomeController();
      var httpContext = MvcMockHelpers.MockHttpContext();
      var routeData = RouteTable.Routes.GetRouteData(httpContext);
      controller.SetMockControllerContext(
      httpContext, routeData, RouteTable.Routes);
      ViewResult result = controller.Index() as ViewResult;

      Assert.AreEqual(result, ErrorResources.Error);

    }
    catch(Exception ex)
    {
      throw ex;
    }
  }

But the problem is this: whenever I am trying to fetch my context in my Utility class to get the Culture, using below line.

var context = new HttpContextWrapper(System.Web.HttpContext.Current);
var routeData = RouteTable.Routes.GetRouteData(context);           
var code = routeData.Values[Constants.CULTURE];

The context which I got return the null routes, means Route and RouteHandler is null and key/value is also 0, so I tried to check this line.

 requestContext.Setup(x => x.RouteData).Returns(new RouteData());

In this line, new RouteData() is null, even though I have registered it before. So how can I get my RouteData() in this case?

1

There are 1 answers

1
Ibrahim ben Salah On

The problem is trivial cuz your are not using the mocked HttpContextBase. Replace the second code block with the following.

var context = MockHttpContext();
var routeData = RouteTable.Routes.GetRouteData(context);           
var code = routeData.Values[Constants.CULTURE];