断言操作重定向到正确的操作/路线?

时间:2008-08-28 14:17:11

标签: asp.net-mvc unit-testing

如何执行操作以确保重定向到正确的操作或路线?

1 个答案:

答案 0 :(得分:10)

public ActionResult Foo()
{
   return RedirectToAction("Products", "Index");
}

[Test]
public void foo_redirects_to_products_index()
{
   var controller = new BarController();
   var result = controller.Foo() as RedirectToRouteResult;

   if(result == null)
      Assert.Fail("should have redirected");

   Assert.That(result.RouteData.Values["Controller"], Is.EqualTo("Products"));
   Assert.That(result.RouteData.Values["Action"], Is.EqualTo("Index"));

}