title: How do I unit test a controller method that has the attribute applied?
body: <p>I create own custom attribute for authentication for web API as per requirement.
  And also I created the unit test project for test web API when I test method it tests successfully but method attribute not tested so how I unit test web API custom attribute.</p>
  
  <p><strong>API code:</strong></p>
  
  <pre><code>[HttpGet]
  [AuthenticateTokenFilter]
  public dynamic CheckUserSeession(string UserID, string AccessID)
  {
      try
      {                   
          TestAPIModel users = new TestAPIModel();
  
          users.Name = "RAM";
          users.ID = "12";
          users.UserName = "M";
  
          var loAPIResponse = new APIResultItem&lt;TestAPIModel&gt; { Data = users, HttpStatusCode = HttpStatusCode.OK, ErrorGUID = null };
          return loAPIResponse;
      }
      catch (Exception Ex)
      {
          throw Ex;
      }
  } 
  </code></pre>
  
  <p><code>[AuthenticateTokenFilter]</code> is my custom attribute, here the code:</p>
  
  <pre><code> public class AuthenticateTokenFilter : ActionFilterAttribute
      {
          public override void OnActionExecuting(HttpActionContext foAction)
          {
              if (!CheckUserAccessID(foAction.ActionArguments))
              {
                  UserLoginResponseModel loResponseModel = new UserLoginResponseModel();
                  loResponseModel.Message = "AuthenticationFailed";
                  if (foAction.ActionArguments != null)
                  {
                      if (foAction.ActionArguments.ContainsKey("AccessID") &amp;&amp; !string.IsNullOrEmpty(foAction.ActionArguments.ContainsKey("AccessID").ToString()))
                          loResponseModel.AccessId = foAction.ActionArguments["AccessID"].ToString();
                      if (foAction.ActionArguments.ContainsKey("UserID") &amp;&amp; !string.IsNullOrEmpty(foAction.ActionArguments.ContainsKey("UserID").ToString()))
                          loResponseModel.UserId = foAction.ActionArguments["UserID"].ToString();
                  }
                  var loInvalidUserSession = new APIResultItem&lt;UserLoginResponseModel&gt; { Data = loResponseModel, HttpStatusCode = HttpStatusCode.Unauthorized, ErrorGUID = null };
                  foAction.Response = foAction.Request.CreateResponse(loInvalidUserSession);
              }
          }  
  
  
  
      public bool CheckUserAccessID(Dictionary&lt;string, object&gt; foActionArguments)
      {
          bool lbIsValidUserSession = false;
          try
          {
              if (foActionArguments.ContainsKey("AccessID") &amp;&amp; foActionArguments.ContainsKey("UserID"))
              {
                  CommonBAL loCommonBAL = new CommonBAL();
                  lbIsValidUserSession = loCommonBAL.CheckUserSession(Convert.ToString(foActionArguments["AccessID"]), Convert.ToString(foActionArguments["UserID"]));
              }
          }
          catch (Exception)
          {
              throw;
          }
          return lbIsValidUserSession;
      }
  }
  </code></pre>
  
  <p><strong>Test code:</strong> </p>
  
  <pre><code>[TestMethod()]
  [AuthenticateTokenFilter]
  public void TestUserSessionSuccess()
  {
      TestAPIModel loResponseModel = new TestAPIModel();
      var loExpectedAPIResponse = new APIResultItem&lt;TestAPIModel&gt; { Data = loResponseModel, HttpStatusCode = HttpStatusCode.OK, ErrorGUID = null };
  
      TestControllerBuilder loTestBuilder = new TestControllerBuilder();
      TNHDefaultWebAPIController loAPIController = new TNHDefaultWebAPIController();
      string lsUserId = "BA893C8B-6DC1-428A-BEF4-0C86389CD58F", lsAccessId = "D95DB230-2A80-4E33-992B-AC46F97DAAB7";
      var loActualResult = (APIResultItem&lt;TestAPIModel&gt;)loAPIController.CheckUserSeession(lsUserId, lsAccessId);
      Assert.AreEqual(loExpectedAPIResponse.HttpStatusCode, loActualResult.HttpStatusCode);
  }
  </code></pre>
  
  <p>How can I test AuthenticateTokenFilter attribute?</p>