开发者

Making mvccontrib testhelper work with actions that get route data automapped to objects

开发者 https://www.devze.com 2022-12-18 16:36 出处:网络
This is ASP.NET MVC v1 (not using the v2 yet) I have a route entry like this: routes.MapRoute( \"Srp\", \"soeg-{searchQuery}/{listingType}\",

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 pr开发者_JAVA技巧oblem 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号