Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何模拟System.Data.IDataReader中的GetValues()方法?_C#_Unit Testing_Rhino Mocks_Datareader - Fatal编程技术网

C# 如何模拟System.Data.IDataReader中的GetValues()方法?

C# 如何模拟System.Data.IDataReader中的GetValues()方法?,c#,unit-testing,rhino-mocks,datareader,C#,Unit Testing,Rhino Mocks,Datareader,如何模拟System.Data.IDataReader中的GetValues()方法 此方法更改传递给它的对象数组,因此它不能简单地返回模拟值 private void UpdateItemPropertyValuesFromReader( object item, IDataReader reader ) { object[] fields = new object[ reader.FieldCount ]; reader.GetValues( fields ); //this

如何模拟System.Data.IDataReader中的GetValues()方法

此方法更改传递给它的对象数组,因此它不能简单地返回模拟值

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
   ...
}

您需要使用接受委托的Expect.Do()方法。然后,该委托需要“执行”某些操作,以代替调用代码。因此,编写一个为您填充fields变量的委托

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
}
private int SetupFields(对象[]字段)
{
字段[0]=100;
字段[1]=“你好”;
返回2;
}
[测试]
public void TestGetValues()
{
MockRepository mocks=新建MockRepository();
使用(mocks.Record())
{
期待
.Call(reader.GetValues(null))
.IgnoreArguments()
.Do(新函数(设置字段))
}    
//在这里验证
}

我注意到你的小错误,但我相信这正是我需要的!感谢you@Ben啊,你得喜欢防火墙。