title: How to mock an SqlDataReader using Moq - Update
body: <p>I'm new to moq and setting up mocks so i could do with a little help. How do I mock up an SqlDataReader using Moq?</p>
  
  <p>Update</p>
  
  <p>After further testing this is what I have so far:</p>
  
  <pre><code>private IDataReader MockIDataReader()
  {
      var moq = new Mock&lt;IDataReader&gt;();
      moq.Setup( x =&gt; x.Read() ).Returns( true );
      moq.Setup( x =&gt; x.Read() ).Returns( false );
      moq.SetupGet&lt;object&gt;( x =&gt; x["Char"] ).Returns( 'C' );
  
      return moq.Object;
  }
  
  private class TestData
  {
      public char ValidChar { get; set; }
  }
  
  private TestData GetTestData()
  {
     var testData = new TestData();
  
     using ( var reader = MockIDataReader() )
     {
         while ( reader.Read() )
         {
             testData = new TestData
             {
                 ValidChar = reader.GetChar( "Char" ).Value
             };
         }
     }
  
     return testData;
  }
  </code></pre>
  
  <p>The issue you is when I do reader.Read in my GetTestData() method its always empty. I need to know how to do something like </p>
  
  <pre><code>reader.Stub( x =&gt; x.Read() ).Repeat.Once().Return( true ) 
  </code></pre>
  
  <p>as per the rhino mock example: <a href="https://stackoverflow.com/questions/1792984/mocking-a-datareader-and-getting-a-rhino-mocks-exceptions-expectationviolationexc">Mocking a DataReader and getting a Rhino.Mocks.Exceptions.ExpectationViolationException: IDisposable.Dispose(); Expected #0, Actual #1</a></p>