title: Moq: unit testing a method relying on HttpContext
body: <p>Consider a method in a .NET assembly:</p>
  
  <pre><code>public static string GetSecurityContextUserName()
  {             
   //extract the username from request              
   string sUser = HttpContext.Current.User.Identity.Name;
   //everything after the domain     
   sUser = sUser.Substring(sUser.IndexOf("\\") + 1).ToLower();
  
   return sUser;      
  }
  </code></pre>
  
  <p>I'd like to call this method from a unit test using the Moq framework. This assembly is part of a webforms solution. The unit test looks like this, but I am missing the Moq code.</p>
  
  <pre><code>//arrange 
   string ADAccount = "BUGSBUNNY";
   string fullADName = "LOONEYTUNES\BUGSBUNNY"; 
  
   //act    
   //need to mock up the HttpContext here somehow -- using Moq.
   string foundUserName = MyIdentityBL.GetSecurityContextUserName();
  
   //assert
   Assert.AreEqual(foundUserName, ADAccount, true, "Should have been the same User Identity.");
  </code></pre>
  
  <p><strong>Question</strong>: </p>
  
  <ul>
  <li>How can I use Moq to arrange a fake HttpContext object with some value like 'MyDomain\MyUser'? </li>
  <li>How do I associate that fake with my call into my static method at <code>MyIdentityBL.GetSecurityContextUserName()</code>? </li>
  <li>Do you have any suggestions on how to improve this code/architecture?</li>
  </ul>