title: Mocking a Stream to Read byte[]
body: <p>My SUT requires a <code>Stream</code> as a parameter, does some work and then returns a <code>byte[]</code>:</p>
  
  <pre><code>public byte[] ProcessRequest(INetworkStream networkStream)
  </code></pre>
  
  <p><code>INetworkStream</code> wraps <code>Stream</code>.</p>
  
  <p>I want to mock the <code>Stream</code> parameter to have control over what bytes it reads so I can test the method with these bytes.</p>
  
  <pre><code>stream.Read(buffer, offset, size);
  </code></pre>
  
  <p><code>Stream.Read(...)</code> returns an <code>int</code> and populates the <code>buffer</code> parameter.</p>
  
  <p>Using <code>Moq</code> how do I fake the result of the call to <code>INetworkStream.Read(...)</code> (So I control both the length of the <code>byte[]</code> as the return value and also the <code>buffer</code> parameter)?</p>
  
  <p>The <a href="https://stackoverflow.com/questions/2176975/how-to-moq-a-networkstream-in-a-unit-test/2177087#2177087">Stream wrapper</a>:</p>
  
  <pre><code>public class FakeNetworkStream : INetworkStream
  {
      private NetworkStream stream;
  
      public FakeNetworkStream(NetworkStream ns)
      {
          if (ns == null) throw new ArgumentNullException("ns");
          this.stream = ns;
      }
  
      public bool DataAvailable
      {
          get
          {
              return this.stream.DataAvailable;
          }
      }
  
      public int Read(byte[] buffer, int offset, int size)
      {
          return this.stream.Read(buffer, offset, size);
      }
  
      public void Write(byte[] buffer, int offset, int size)
      {
          this.stream.Write(buffer, offset, size);
      }
  }
  </code></pre>