using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Web.Mvc; using NUnit.Framework; namespace UnitTests.Controllers { public static class ActionResultExtensions { #region Routing Assertions /// /// Assert that the action in the result's Route Data is /// /// The ActionResult to make the assertion against. /// The name of the Action to assert public static void AssertAction(this ActionResult result, string actionName) { Assert.AreEqual(actionName, ((RedirectToRouteResult)result).Values["action"]); } #region AssertRoute /// /// Assert that the result's routing data matches that given in the action Expression /// /// Controller Type /// The ActionResult to make the assertion against. /// Expression containing the route you'd like to assert public static void AssertRoute(this ActionResult result, Expression> action) where TController : Controller { // Figure out the action name var actionName = ((MethodCallExpression)action.Body).Method.Name; // Figure out the controller name var controllerName = typeof(TController).Name; if (controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)) controllerName = controllerName.Remove(controllerName.Length - 10, 10); AssertRoute(result, controllerName, actionName); } /// /// Assert that the result's routing data matches the Action actionName /// on the controller type TController /// /// Controller Type /// The ActionResult to make the assertion against. /// Name of the action to match public static void AssertRoute(this ActionResult result, string actionName) where TController : Controller { var controllerName = typeof (TController).Name; AssertRoute(result, controllerName, actionName); } private static void AssertRoute(this ActionResult result, string controllerName, string actionName) { var routeValues = ((RedirectToRouteResult) result).Values; Assert.AreEqual(controllerName, routeValues["controller"]); Assert.AreEqual(actionName, routeValues["action"]); } #endregion #endregion /// /// Assert that the result's View Name matches expectedViewName /// /// /// The ActionResult to make the assertion against. /// (Assumed to be a ViewResult) /// /// The name of the View to match public static void AssertViewName(this ActionResult result, string expectedViewName) { Assert.AreEqual(expectedViewName, ((ViewResult)result).ViewName); } /// /// Assert that an object in the result's ViewData matches the expectedObject /// /// /// The ActionResult to make the assertion against. /// (Assumed to be a ViewResult) /// /// The name of the View to match public static void AssertViewData(this ActionResult result, object expectedObject) { AssertViewData(result, null, expectedObject); } /// /// Assert that an object in the result's ViewData matches the expectedObject /// /// /// The ActionResult to make the assertion against. /// (Assumed to be a ViewResult) /// /// The ViewData key in which the expected ViewData to be matched is stored /// The name of the View to match public static void AssertViewData(this ActionResult result, string key, object expectedObject) { var actualViewData = ((ViewResult)result).ViewData; if (string.IsNullOrEmpty(key)) { if (actualViewData.Model != null) Assert.AreSame(expectedObject, actualViewData.Model); else Assert.AreSame(expectedObject, actualViewData); } else Assert.AreSame(expectedObject, actualViewData[key]); } /// /// Assert that a group of objects in the result's ViewData matches those in the mappings dictionary. /// /// /// The ActionResult to make the assertion against. /// (Assumed to be a ViewResult) /// /// /// A dictionary containing keys and values to be validated against the result's ViewData /// public static void AssertViewDataMappings(this ActionResult result, IDictionary mappings) { foreach (var key in mappings.Keys) AssertViewData(result, key, mappings[key]); } } }