I am writing unit tests for my data access layer. To achieve this I have created a wrapper for SqlCommand (ISqlCommand) so that I can mock its functionality.
ISqlCommand command = _connection.GetSqlCommand(sqlCommand);
In one of the methods I am testing, SqlCommand's ExecuteReader method is being called and I have to return a SqlDataReader.
SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow);
In this same method reader.Read will get called
if (reader.Read())
{
someVariable = reader.GetString(1);
}
What I want is to be able to return a SqlDataReader object from the mocked command.ExecuteReader() with a value present for me to Read. Can it be done? It seems that SqlDataReader can only be instantiated from an actual SqlCommand.ExecuteReader being run and returning a SqlDataReader. Full relevant code to be tested.
ISqlCommand command = _connection.GetSqlCommand(sqlCommand);
using (command)
{
SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow);
if (reader.Read())
{
dbVersion = reader.GetString(1);
}
}
EDIT: To clear about what I am asking. SqlDatareader has no public constructor. As far as I can tell I cannot write any tests using that class as I cannot instantiate it without making a legitimate call to a database using SqlCommand. Even trying to create an interface of a SqlDataReaderWrapper will no help me as the problem is the same. I am not trying to write an integration test (making actual calls to a DB) and so the DataReader seems impossible to test as is. My question is, is there anything i can do, to put values into a SqlDataReader in this situation?