public

Method for verifying that a method contains an Attribute

  • Download Gist
MvcActionFilterTestHelper.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/// <summary>
/// Verifies the controller action, contains an attribute of the specified attributeType.
/// </summary>
/// <param name="controller">The controller.</param>
/// <param name="action">The action method.</param>
/// <param name="attributeType">Type of the attribute to look for.</param>
/// <returns>Returns true if the attribute was present on the action. Otherwise false.</returns>
public static bool VerifyControllerActionAttribute(this Controller controller, Func<ActionResult> action, Type attributeType)
{
MethodInfo methodInfo = action.Method;
object[] attributes = methodInfo.GetCustomAttributes(attributeType, true);
 
return attributes.Any(a => a.GetType() == attributeType);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.