2

I found articles on how to unit test routes from Stephen Walther and from Scott Gu in prior versions of ASP.NET MVC, but it doesn't work in ASP.NET MVC Beta 1. If I need to use a mocking framework, I'd prefer a solution that uses Rhino Mocks.

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Mathias F
  • 15,906
  • 22
  • 89
  • 159

1 Answers1

2

I use Visual Studio unit tests, thats why I could not use the Helper Tim mentioned out of the box. (NUnit required). Anyway the article showed the code I needed. Actualy in the "nobody wants to use this ugly code" section.

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


        var httpContext = MockRepository.GenerateStub<HttpContextBase>();

        httpContext.Stub(x => x.Request).Return(MockRepository.GenerateStub<HttpRequestBase>());

        httpContext.Request.Stub(x => x.PathInfo).Return("");

        httpContext.Request.Stub(x => x.AppRelativeCurrentExecutionFilePath).Return("~/foo/bar");



        var routeData = routes.GetRouteData(httpContext);



        Assert.AreEqual(routeData.Values["controller"],"foo");
Mathias F
  • 15,906
  • 22
  • 89
  • 159