如何单元测试MVC重定向?
public ActionResult Create(Product product) { _productTask.Save(product); return RedirectToAction("Success"); } public ActionResult Success() { return View(); }
public static void RenderView(this Controller self,string action) { typeof(Controller).GetMethod("RenderView").Invoke(self,new object[] { action} ); }
似乎很奇怪,要做到这一点,特别是当MVC团队表示他们正在写框架是可测试的。
解决方法
[TestFixture] public class RedirectTester { [Test] public void Should_redirect_to_success_action() { var controller = new RedirectController(); var result = controller.Index() as RedirectToRouteResult; Assert.That(result,Is.Not.Null); Assert.That(result.Values["action"],Is.EqualTo("success")); } } public class RedirectController : Controller { public ActionResult Index() { return RedirectToAction("success"); } }