chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I'm trying to mock IDataReader, found some code in previous posts that should work, but unfortunatly I can't get it to work.

I'm using the microsoft daab and the sqlhelper class.

My code is as follows:
Mock sqlHelperMock = MockManager.Mock(typeof(SqlHelper));
MockObject mockIDataReader = MockManager.MockObject(typeof(IDataReader));
sqlHelperMock.ExpectAndReturn("ExecuteReader", mockIDataReader as IDataReader);

mockIDataReader.ExpectAndReturn("Read", true);

// call some legacy code.

myObj.doSomeDataAccess..


In the dataaccess method code looks like:
IDataReader someDataReader = SqlHelper.ExecuteReader(ConnectionString,
CommandType.StoredProcedure,
"GetSomeData",
sqlParams);

if (someDataReader != null)
if (someDataReader.Read())

someDataReader should be mocked, but is always null so no read is called and mocking fails..

Should this work or am i doing something wrong?
asked by Bert van Kammen (680 points)

5 Answers

0 votes
Bert,
To get the actual mocked object (and not the mock that controlls how that object behaves) use the mock.Object property :
sqlHelperMock.ExpectAndReturn("ExecuteReader", mockIDataReader.Object as IDataReader); 

other wise you are returning null :-)
answered by scott (32k points)
0 votes
Hello Scott,

I get the following exception when trying your approach:
No method ExecuteReader in type Microsoft.ApplicationBlocks.Data.SqlHelper returns MockIDataReader

Regards,
Bert
answered by Bert van Kammen (680 points)
0 votes
you have probably written
sqlHelperMock.ExpectAndReturn("ExecuteReader", mockIDataReader)
you need to write
sqlHelperMock.ExpectAndReturn("ExecuteReader", mockIDataReader.Object as IDataReader);
answered by scott (32k points)
0 votes
Scott,

You're first reply was already clear at that point so
this is exactly what I've written:

sqlHelperMock.ExpectAndReturn(methodName, mockIDataReader.Object as IDataReader);

while executing the test I get the following exception:
No method ExecuteReader in type Microsoft.ApplicationBlocks.Data.SqlHelper returns MockIDataReader

I resolved it for now by using reflection and passing a SqlDataReader instance as IDataReader to sqlHelperMock.ExpectAndReturn.

to mock the SqlDataReader I use
Mock dataReaderMock = MockManager.MockAll(typeof(SqlDataReader));

So it works for now, but I was hoping on an easier approach to get this working.

____________________________
Regards,
Bert
answered by Bert van Kammen (680 points)
0 votes
You are right, my bad.
I have looked at the API and ExecuteReader returns an SqlDataReader type and not an IDataReader. As thus you must create an SqlDataReader object to return. (This is why your solution works, and BTW there is no need to cast to IDataReader).

It should be possible to create an SqlDataReader by using MockObject. If you cannot (I haven't tried it) Then using Reflection is a good option.
answered by scott (32k points)
...