Update:

This fix is included in the current release of MVC-Contrib.

I have been using the MVCContrib RouteTestingExtensions today for the first time and they have been working really well.... that is until I started writing a custom route that returns a complex type to the RouteValueDictionary.

The tests that I had assumed would work and pass were like the following:

    [TestFixture, Category("Routing")]
    public class When_Url_to_route_is_dynamic_with_trailing_slash_and_no_html_extension : DynamicContentRouteSpecification
    {
        public override void Given()
        {
            base.Given();
            currentUrl = "~/business/something/";
            dynamicUrl = new DynamicUrl(currentUrl.Substring(2));
        }

        [Test]
        public void Then_the_current_url_should_map_to_DynamicContentController_Render_action()
        {
            route.ShouldMapTo<DynamicContentController>(a => a.Render(dynamicUrl));
        }
    }

In my code sample above the DynamicUrl is a class that wraps additional logic around the passed in url, and I have a ToString() method on it that simply returns the original url value.

This test kept blowing up, and after looking at the tests and code from MVCContrib I found the code that was causing all of the problems.

        public static object GetValue(this RouteValueDictionary routeValues, string key)
        {
            foreach(var routeValueKey in routeValues.Keys)
            {
                if(string.Equals(routeValueKey, key, StringComparison.InvariantCultureIgnoreCase))
                    return routeValues[routeValueKey] as String;
            }

            return null;
        }

This GetValue method within the RouteTestingExtensions would work as expected until the return "as String".  As we know "as String" returns a string if it can be converted or null if it can not.

To resolve this issue I amended the MVCContrib code to the following:

        public static object GetValue(this RouteValueDictionary routeValues, string key)
        {
            foreach(var routeValueKey in routeValues.Keys)
            {
                if(string.Equals(routeValueKey, key, StringComparison.InvariantCultureIgnoreCase))
                    return routeValues[routeValueKey].ToString();
            }

            return null;
        }

After making the changes and running the MVCContrib tests, they all passed.

I then re-built and referenced the new assembly builds in my project and HAZZA! my tests started passing.

I have uploaded the patch and you can download it here

If anyone has any feedback on this, or if I have gone about using the helper the wrong way please leave me a comment.

David the Ninja