ASP MVC 5 测试 RedirectToAction

ASP MVC 5 testing RedirectToAction

我正在测试控制器是否没有参数,它应该重定向到操作 "Index"

// Controller code
return RedirectToAction("Index");

// Unit test code
ActionResult result = Controller.Action(null);
Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
Assert. // HERE i need to test it is going to Index, how do I do that?

您需要像这样查看 RouteValues 字典:

Assert.AreEqual("Index", (result as RedirectToRouteResult).RouteValues["action"]);