Is there a way to convert IDataReader object to SqlDataReader object? I am trying to break database dependency to one of API, by intercepting SqlHelper by with my own IDataReader (since I can't create a SqlDataReader object).
This is how my test case looks like (and evaluating TypeMock):
Isolate.Fake.StaticMethods(typeof(SqlHelper));
Isolate.WhenCalled(() => SqlHelper.ExecuteReader(string.Empty, CommandType.StoredProcedure, string.Empty))
.WillReturn(myDataGenerator.ToDataReader() as SqlDataReader);
This get compiled with no problem, but does not return any rows. When I debug I see that it does not have any rows at all. Any thoughts?
Thanks in advance.
Edit(1): Since conversion is not possible, is it possible to mock SqlDataReader.
No you can't convert a
IDataReaderto aSqlDataReader.SqlDataReaderis aIDataReader- anSqlDataReadercan be converted to anIDataReadernot the otherway around.Your real problem is that your code wasn't written to be testable. Really
SqlHelpershould be an instance class is passed in and can be mocked and returns anIDataReader. Or the method or methods that you are testing can be refactored so they don't rely onSqlHelperdireclty and takeIDataReader's.However if as I suspect you don't control this code, you can create your own DataReader that inherits from
SqlDataReader. All theIDataReadermethods are virtual so you can override them your self.