views:

105

answers:

1

This is ASP.NET MVC v1 (not using the v2 yet)

I have a route entry like this:

            routes.MapRoute(
            "Srp",
            "soeg-{searchQuery}/{listingType}",
            new { controller = "Srp", action = "Search", listingType = string.Empty },
            new { listingType = "privat|forhandler|"}
            );

and an action to match it:

        public ActionResult Search(QueryParameters queryParameters)

It works perfectly - the mvc framework knows to map the searchQuery and listingType onto the two properties of the QueryParameters object with the same names.

My problem is unit testing. I'm using Mvccontrib project and LOVING the ShouldMapTo method:

        [Test]
    public void RegisterSrpRoutes_SoegWithKeywordAndValidListingType_ShouldMapCorrectly()
    {
        var queryParameters = new QueryParameters {SearchQuery = "hest", ListingType = "privat"};
        "~/soeg-hest/privat".ShouldMapTo<SrpController>(controller => controller.Search(queryParameters));
    }

It doesn't work though! I used to have specific parameters on my action like this:

        public ActionResult Search(string searchQuery, string listingType)

which worked (obviously the unittest would try and map to Search with two parameters (strings) instead of this one object.

Does anyone have an idea of how to solve the problem, short of going back to writing all properties as parameters. The mvc automapping of properties rocks, but i'm hoping there is some way i can have mvccontribs testhelper work with that as well.

A: 

It's been a while since I looked at this code, but I believe that it does a .Equals call on the parameter you send. For primitive types this is easy, but for you parameter object, try implementing the Equals override and have it test the equality of each of the properties.

Ben Scheirman
I think you're right. It's a bit of a pain and frankly I think I'm just going to skip the ShouldMapTo instead *sniff*
Per Hornshøj-Schierbeck