mvccontrib test helper and verifying http post routes and parameters

后端 未结 2 543
栀梦
栀梦 2021-01-05 11:02

In my Asp.net MVC app, I have two methods on a controller, one for when the user first arrives on the view and then one when they submit the form on said view.



        
相关标签:
2条回答
  • 2021-01-05 11:43

    Here's an example. Assuming you have the following action:

    public AccountController : Controller
    {
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Foo(string id) 
        {
            return View();
        }
    }
    

    And the following route registered:

    RouteTable.Routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "home", action = "index", id = "" }
    );
    

    You could test it like this:

    var routeData = "~/account/foo".WithMethod(HttpVerbs.Post);
    routeData.Values["id"] = "123";
    routeData.ShouldMapTo<AccountController>(c => c.Foo("123"));
    

    Some tweaking might be necessary to include the second Account argument you have.

    0 讨论(0)
  • 2021-01-05 12:02

    Using the mvccontrib helper syntax:

    "~/account/foo".WithMethod(HttpVerbs.Post).ShouldMapTo<AccountController>(a => a.foo(null));
    

    You pass null as the Foo(string id, Account accountToFoo) method is never executed as part of the routing test.

    0 讨论(0)
提交回复
热议问题