title: Hot to Add Cookies to the httpcontextbase from unit test and accessing from controller
body: <p>I am trying to unit test one of my controller action method but having issue with HttpContext.</p>
  
  <p>Below is the Action method code which reads the id from the cookie.</p>
  
  <pre><code> public decimal GetId()
      {
          string result = "0"; 
          try
          {
          HttpCookie authCookie = HttpContext.Request.Cookies["Id"];
       //Getting error in above line of code saying the Httpcontext is null
          FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
                result = ticket.UserData.Substring(0, ticket.UserData.IndexOf("|"));
  
          }
          catch (Exception ex)
          {
              LoggerHelper.LogError("GetID", ex.ToString());
          }
  
          return Convert.ToDecimal(result);
      }
  </code></pre>
  
  <p>And when I try to unit test the controller Action method getting error in below line of code </p>
  
  <pre><code>   HttpCookie authCookie = HttpContext.Request.Cookies["Id"];
  </code></pre>
  
  <p>HttpContext becoming null even though I fake the httpcontextbase and added cookie to it in my Unit test code.</p>
  
  <p>Please help. I spent whole day but still unable to figure it out why HttpContext becoming null.</p>
  
  <p>Thanks in advance,
  VJ</p>