How do I check if a route / action URL matches a query string?

In my unit tests, I am trying to use the following code:

/* Test setup code */
_routes = RouteTable.Routes;
MvcApplication.RegisterRoutes(_routes); //set up the routes as they would be in actual application
/* test code */
Expression<Func<SearchController, ActionResult>> actionFunc;
actionFunc = action => action.Results("x", 3, null);
RouteTestingExtensions.Route(
   "~/Search/Results?searchText=x"
).ShouldMapTo<SearchController>(actionFunc);

The problem is that this does not work with "Expected Results for Results: searchText = x"

Does anyone have a solution that allows me to verify that the URL (with the query string) allows the correct controller, action, and arguments?

FYI, I don’t have explicit route settings in Global.asax.cs, since the default route works for a real application - it just doesn’t work in this test.

+5
source share
2 answers

IMHO unit test . , , . Microsoft ().

MVCContrib.TestHelper . , , , URL SEO:

routes.MapRoute(
    "Custom",
    "foo/{startPage}/{endPage}",
    new 
    { 
        controller = "Search", 
        action = "Results", 
    }
);

:

public class SearchController : Controller
{
    public ActionResult Results(int startPage, int endPage)
    {
        return View();
    }
}

:

"~/foo/10/20".ShouldMapTo<SearchController>(c => c.Results(10, 20));

, Search, Results startPage endPage .

+9

All Articles