title: Trying to Mock a data Service which returns a IDataReader
body: <p>I'm trying to mock (using Rhino.Mocks) a DataService which returns an IDataReader. I Can mock the IDataReader (<a href="https://stackoverflow.com/questions/1792984/mocking-a-datareader-and-getting-a-rhino-mocks-exceptions-expectationviolationex">StackOverFlow Q 1792984</a>) but if I then Stub out A Data Service which returns this DataReader and inject it into the method I am trying to test it the stub returns only a null Data Reader.</p>
  
  <p>IDataService</p>
  
  <pre><code>public interface IDataService
  {
   SqlDataReader ExecuteReader(string cmdText, CommandType commandType);
   &lt;rest of definition ommited
  }
  </code></pre>
  
  <p>Unit test</p>
  
  <pre><code>    [TestMethod]
      public void GetCustomer_Pass()
      {
          //Arrange
          var customer = new Customer()
                         {
                           Id = 1, FirstName = "Patrick",
                           LastName = "Hastings", ProfilePictureURL = ""
                         };
  
          var mockDataService = MockRepository.GenerateStub&lt;IDataService&gt;();
  
          var reader = MockRepository.GenerateStub&lt;IDataReader&gt;();
          reader.Stub(x =&gt; x.Read()).Repeat.Once().Return(true);
          reader.Stub(x =&gt; x.Read()).Return(false);
          reader.Stub(x =&gt; x["Id"]).Return(customer.Id);
          reader.Stub(x =&gt; x["FirstName"]).Return(customer.FirstName);
          reader.Stub(x =&gt; x["LastName"]).Return(customer.LastName);
          reader.Stub(x =&gt; x["ProfilePictureURL"]).Return(customer.ProfilePictureURL);
  
          mockDataService.Stub( s =&gt;
              s.ExecuteReader(string.Format("select FirstName, LastName,ProfilePictureURL from customer where Id = {0}", customer.Id),
                              CommandType.Text))
                              .Return(reader as SqlDataReader);
  
          var custRepository = new CustomerRepository(mockDataService);
  
          //Act
          var customer2 = custRepository.GetCustomer(1);
          //Assert
          Assert.AreEqual(customer.FirstName, customer2.FirstName,"FirstName Mismatch");
          Assert.AreEqual(customer.LastName, customer2.LastName, "LastName Mismatch");
      }
  </code></pre>
  
  <p>Method UnderTest</p>
  
  <pre><code>    public CustomerRepository(IDataService dataService) //Constructor
      {
          if (dataService == null) throw new ArgumentNullException("dataService", "dataService cannot be null.");
          this.dataService = dataService;
      }
  
      public Customer GetCustomer(int id)
      {
          var sql =string.Format("select FirstName, LastName, ProfilePictureURL from customer where Id = {0}",id);
          var dr = dataService.ExecuteReader(sql, CommandType.Text);
          Customer customer ;
          using (dr)
          {
              if (!dr.Read()){return null;}
              customer = new Customer
                           {
                               FirstName = dr["FirstName"].ToString(),
                               LastName = dr["LastName"].ToString(),
                               ProfilePictureURL = dr["ProfilePictureURL"].ToString()
                           };
          }
          return customer;
      }
  </code></pre>
  
  <p>This issue that I get is that when "dataService.ExecuteReader(sql, CommandType.Text);" should return the mocked IDataReader but instead returns Null - any pointers as to what I am doing wrong would be gratefully appreciated.</p>