title: .NET: how can I unit test this method?
body: <p>(Similar to <a href="https://stackoverflow.com/questions/4214812">.NET how to fudge the HttpContext object</a>!)</p>
  
  <p>I have a method that I want to test:</p>
  
  <pre><code>    public void MyMethod()
      {
          if (HttpContext.Current.Application["myValue"] != null)
              Console.WriteLine("Boo!");
      }
  </code></pre>
  
  <p>How can I write a test WITHOUT refactoring the method so that the <code>Console.WriteLine</code> is hit?</p>
  
  <p>I tried with:</p>
  
  <pre><code>        TextWriter tw = new StringWriter();
          HttpWorkerRequest wr = new SimpleWorkerRequest("/webapp", @"path...", "logon.asp", "", tw);
          HttpContext.Current = new HttpContext(wr);
          HttpContext.Current.Application.Add("KeyValue", "myValue");
          MyMethod();
  </code></pre>
  
  <p>but HttpContext.Current.Application.count is always zero, I cannot add values to it!</p>