21 Jul 2010, 00:02
Headshot-sm_pragsmall

Alexander DiMauro (5 posts)

The route testing on page 97 doesn’t seem to work. I get an AssertionException: The URL did not match any route. I even tried a simple route, and tested the Index() action, and get the same problem.

On the MvcContrib home page, in their example they actually recreate routes in the SetUp() at the top of the TestFixture. I tried that, and my Index test works, but in the Edit test, I get AssertionException: Expected Edit but was Edit?title=Get-A-LOT-MORE-milk.

I noticed in the downloaded code, this test is not even included (unless I missed it). Are we supposed to follow along with the section, or was this just a quick demo? How do we get the route test to actually work in the case of the Edit(string title) action?

13 Aug 2010, 02:07
Headshot_pragsmall

Jonathan McCracken (35 posts)

Alexander,

Looking into this myself and the route test is missing from the downloadable code.

Here is the solution:

    [TestFixture]
    public class RouteDefinitionsTest
    {
        [SetUp]
        public void setup()
        {
            var routes = RouteTable.Routes;
            routes.Clear();
            RouteDefinitions.AddRoutes(routes);
        }
        
        [Test]
        public void Should_Route_To_Edit_Page_With_Title()
        {            
            "~/Todo/Edit".
            ShouldMapTo<TodoController>(x => x.Edit(null));
        }
    }

So MVC Contrib does not map parameters, that’s up to model binding (my mistake) which can be tested separately. So in these cases where you have parameters you need to pass @null@ and then your test will pass.

Course you’ll need to add @RouteDefinitions@ to your Web project as well which is here:

public class RouteDefinitions
{
  public static void AddRoutes(RouteCollection routes)
  {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
     "Default", // Route name
     "{controller}/{action}/{id}", // URL with parameters
     new {controller = "Home", action = "Index", id = ""} // Parameter defaults
    );
  }
}

I just extracted this out of the Global.asax.cs file, so now that needs to change to the following:

public static void RegisterRoutes(RouteCollection routes)
{
  RouteDefinitions.AddRoutes(routes);
}

This will go into my updates for the chapter and final code download.

Also, glad to hear (from a different thread in this forum) that you’re enjoying the book.

Thanks,

Jonathan

13 Aug 2010, 02:07
Headshot_pragsmall

Jonathan McCracken (35 posts)

modified my original post, but it didn’t go to the top of the forum posts - so I’ve added this message to game the system :)

  You must be logged in to comment