title: Is Mocking able to replace functionality wrapped inside a method?
body: <p>I'm trying to define a way to simulate an case on accessing into a database without accessing... That's probably sounds quite crazy, but it's not.</p>
  
  <p>Here is an example about a method i would like to test:</p>
  
  <pre><code>    public IDevice GetDeviceFromRepository(string name)
      {
          IDevice device = null;
          IDbConnection connection = new SqlConnection(ConnectionString);
          connection.Open();
          try
          {
              IDbCommand command = connection.CreateCommand();
              command.CommandText = string.Format("SELECT DEVICE_ID,DEVICE_NAME FROM DEVICE WHERE DEVICE_NAME='{0}'", name);
              IDataReader dataReader = command.ExecuteReader();
              if(dataReader.NextResult())
              {
                  device = new Device(dataReader.GetInt32(0),dataReader.GetString(1));
              }
          }
          finally
          {
              connection.Close();
          }
          return device;
      }
  </code></pre>
  
  <p>I'm pretending to mock IDataReader so i can control what's being read. Something like that (using Moq framework):</p>
  
  <pre><code>    [TestMethod()]
      public void GetDeviceFromRepositoryTest()
      {
          Mock&lt;IDataReader&gt; dataReaderMock = new Mock&lt;IDataReader&gt;();
          dataReaderMock.Setup(x =&gt; x.NextResult()).Returns(true);
          dataReaderMock.Setup(x =&gt; x.GetInt32(0)).Returns(000);
          dataReaderMock.Setup(x =&gt; x.GetString(1)).Returns("myName");
          Mock&lt;IDbCommand&gt; commandMock = new Mock&lt;IDbCommand&gt;();
          commandMock.Setup(x =&gt; x.ExecuteReader()).Returns(dataReaderMock.Object);
          Mock&lt;RemoveDeviceManager&gt; removeMock = new Mock&lt;RemoveDeviceManager&gt;();
          removeMock.Setup()
          RemoveDeviceManager target =new RemoveDeviceManager(new Device(000, "myName")); 
          string name = string.Empty; 
          IDevice expected = new Device(000, "myName"); // TODO: Initialize to an appropriate value
          IDevice actual;
          actual = target.GetDeviceFromRepository(name);
          Assert.AreEqual(expected.SerialNumber, actual.SerialNumber);
          Assert.AreEqual(expected.Name, actual.Name);
      }
  </code></pre>
  
  <p>My question is whether i can force method GetDeviceFromRepository to replace IDataReader by mocked one.</p>