所以这是我的小例子:
public ActionResult Index(int id)
{
if (1 != id)
{
return RedirectToAction("asd");
}
return View();
}
和测试:
[TestMethod]
public void TestMethod1()
{
HomeController homeController = new HomeController();
ActionResult result = homeController.Index(10);
Assert.IsInstanceOfType(result,typeof(RedirectToRouteResult));
RedirectToRouteResult routeResult = result as RedirectToRouteResult;
Assert.AreEqual(routeResult.RouteValues["action"], "asd");
}
[TestMethod]
public void TestMethod2()
{
HomeController homeController = new HomeController();
ActionResult result = homeController.Index(1);
Assert.IsInstanceOfType(result, typeof(ViewResult));
}
编辑:
验证结果类型为 ViewResut 后,您可以对其进行强制转换:
ViewResult vResult = result as ViewResult;
if(vResult != null)
{
Assert.IsInstanceOfType(vResult.Model, typeof(YourModelType));
YourModelType model = vResult.Model as YourModelType;
if(model != null)
{
//...
}
}