How to mock an SqlDataReader using Moq - Update
- by Simon G
Hi,
I'm new to moq and setting up mocks so i could do with a little help.
Title says it all really - how do I mock up an SqlDataReader using Moq?
Thanks
Update
After further testing this is what I have so far:
private IDataReader MockIDataReader()
{
var moq = new Mock<IDataReader>();
moq.Setup( x => x.Read() ).Returns( true );
moq.Setup( x => x.Read() ).Returns( false );
moq.SetupGet<object>( x => x["Char"] ).Returns( 'C' );
return moq.Object;
}
private class TestData
{
public char ValidChar { get; set; }
}
private TestData GetTestData()
{
var testData = new TestData();
using ( var reader = MockIDataReader() )
{
while ( reader.Read() )
{
testData = new TestData
{
ValidChar = reader.GetChar( "Char" ).Value
};
}
}
return testData;
}
The issue you is when I do reader.Read in my GetTestData() method its always empty. I need to know how to do something like
reader.Stub( x => x.Read() ).Repeat.Once().Return( true )
as per the rhino mock example: http://stackoverflow.com/questions/1792984/mocking-a-datareader-and-getting-a-rhino-mocks-exceptions-expectationviolationexc