我正在尝试对自定义 ActionFilterAttribute 进行单元测试。例如:
public class ActionAuthorizationAttribute: ActionFilterAttribute
{
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.HttpContext.Response.Redirect("SecurityException");
}
}
如何编写一个单元测试来测试任何具有此属性的操作是否会重定向到 SecurityException。
我的尝试
[TestClass]
public class TestControllerResponse
{
[TestMethod]
public void TestPermissionAccepted()
{
var controller = new TestController();
var result = controller.Index() as RedirectToRouteResult;
Assert.IsNotNull(result);
Assert.AreEqual("SecurityException", result.RouteValues["action"]);
}
}
protected class TestController : Controller
{
[ActionAuthorization]
public ActionResult Index()
{
return RedirectToAction("UnfortunatelyIWasCalled");
}
}
不幸的是,测试失败了。索引操作重定向到“UnfortunatelyIWasCalled”而不是“SecurityException”。