title: Mocking HTTPResponse in a unit test
body: <p>I'm trying to create unit tests for some legacy code .
  One of the classes that I have to test is called FileDownloader which has just the following one method :</p>
  
  <pre><code>public void Transmit(string fileName, HttpResponse response, DownloadFileType fileType, byte[] content)
  {
      response.Clear();
      response.ClearHeaders();
      response.ContentType = "application/xls";
      response.AddHeader("content-disposition", "attachment; filename=" + HttpContext.Current.Server.UrlEncode(fileName));
      response.BinaryWrite(content);
      response.End();
      response.Flush();
  }
  </code></pre>
  
  <p>I'm not allowed to refactor this code ( which would have been ideal ! ).</p>
  
  <p>To test this I decided to create a fake HttpContext based on the article below</p>
  
  <p><a href="http://blogs.vertigo.com/personal/aanttila/Blog/Lists/Posts/Post.aspx?List=552eb137-35b0-44f9-bee8-641e08159e2b&amp;ID=27&amp;Web=f31077d6-bea7-416f-82e9-e4103a9c0670" rel="noreferrer">Click this</a></p>
  
  <p>With this I'm able to get a fake HttpContext during my test execution , however there are issues with faking the HttpResponse .</p>
  
  <p>Here's how my test looks like :</p>
  
  <pre><code>[SetUp]
  public void SetUp()
  {
      mocks = new MockRepository();            
      FakeHttpContext.CreateFakeHttpContext();
  }
  
  [Test]
  public void ShouldTransmitHttpResponseInTheSpecifiedFormat()
  {
      FileDownloader downloader = new FileDownloader();
      string path = "..\\..\\Fakes\\DummyDownloadReportsTemplate.xls";
      byte[] bytes = ReadByteArrayFromFile(path);
      downloader.Transmit("test.xls", new HttpResponse(new StringWriter()), DownloadFileType.Excel, bytes);
  }
  </code></pre>
  
  <p>I'm passing a custom created HTTPResponse object to the method.
  This throws the following exception when it hits the "response.BinaryWrite(content)" line :</p>
  
  <p>System.Web.HttpException : OutputStream is not available when a custom TextWriter is used.</p>
  
  <p>I'm not sure what exactly should I be asserting here .. hence there no asserts in the test.
  Is this the correct way to test this method ... any ideas . please advise ?</p>
  
  <p>Thanks</p>