I am trying to unit test to create a special ActionFilterAttribute. For instance:
public class ActionAuthorizationAttribute: ActionFilterAttribute { protected override void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.HttpContext.Response.Redirect("SecurityException"); } }
How can I write a unit test that will verify that any action that has this attribute will be redirected to a SecurityException.
My attempt
[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"); } }
Unfortunately, the test failed. The index action is redirected to "SorryIWasCalled" instead of "SecurityException".
source share