views:

461

answers:

2

How do I mock the method GetValues() in System.Data.IDataReader?

This method changes the array of objects passed to it, so it can’t simply return a mocked value.

private void UpdateItemPropertyValuesFromReader( object item, IDataReader reader )
{
    object[] fields = new object[ reader.FieldCount ];
    reader.GetValues( fields ); //this needs to be mocked to return a fixed set of fields


    // process fields
   ...
}
A: 

I think you want the Do() handler.

Jon Skeet
+8  A: 

you need to use the method Expect.Do() which takes a delegate. this delegate then needs to 'do' something, in place of the calling code. Therefore, write a delegate that populates the fields variable for you.

private int SetupFields( object[] fields )
{
    fields[ 0 ] = 100;
    fields[ 1 ] = "Hello";
    return 2;
}

[Test]
public void TestGetValues()
{
    MockRepository mocks = new MockRepository();

    using ( mocks.Record() )
    {
        Expect
            .Call( reader.GetValues( null ) )
            .IgnoreArguments()
            .Do( new Func<object[], int>( SetupField ) )
    }    

    // verify here
}
Ben Laan
I notice your small typo, but I believe this is what I need! thank you
Devtron
I can't post a < character due to a corporate firewall 'policy'..
Ben Laan
@Ben Ahh you gotta to love firewalls.
Nathan W