I currently have the requirement to set the Request.AcceptTypes property for a controller test.  AFAIK there is currently no way of doing this within the MVC-Contrib TestControllerBuilder.

I updated their latest trunk and have created a patch file in case you, or the MVC-Contrib guys want to use it.

Heres the test:

        [Test]
        public void CanSpecifyRequestAcceptTypes()
        {
            builder.AcceptTypes = new[] {"some/type-extension"};
            var controller = new TestHelperController();
            builder.InitializeController(controller);
            Assert.That(controller.HttpContext.Request.AcceptTypes, Is.Not.Null);
            Assert.That(controller.HttpContext.Request.AcceptTypes.Length, Is.EqualTo(1));
            Assert.That(controller.HttpContext.Request.AcceptTypes[0], Is.EqualTo("some/type-extension"));
        }

 

Heres the code

		protected void SetupHttpContext()
		{
			...
SetupResult.For(request.AcceptTypes).Do((Func<string[]>)(() => AcceptTypes));

.....
		}

Download the patch: click here

 

Dave the Ninja


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

 

 


About this blog

Dave the Ninja is an Alt.net practitioner from Glasgow in Scotland (UK). He rants about all things from coding to Kung Fu with some swearing thrown in for good measure.

Dave the Ninja has been programming professionally with Microsoft technologies for the past 9 years starting and is now working primarily with C# 3.5, MVC.net and Resharper.

Dave the Ninja has been practicing Chinese internal arts for the past 15 and a half years, mainly Taiji Quan, Neijia Quan , Chi Na and Qigong.

I have joined Anti-IF Campaign

My Communities/Projects

Flickr PhotoStream


Sponsors