【问题标题】:How do I mock the method GetValues() in System.Data.IDataReader?如何在 System.Data.IDataReader 中模拟 GetValues() 方法?
【发布时间】:2008-11-27 22:12:59
【问题描述】:

如何模拟 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
   ...
}

【问题讨论】:

    标签: c# unit-testing rhino-mocks datareader


    【解决方案1】:

    您需要使用带有委托的 Expect.Do() 方法。然后这个委托需要“做”一些事情来代替调用代码。因此,编写一个为您填充字段变量的委托。

    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 Ahh 你一定会喜欢防火墙。
    猜你喜欢
    • 1970-01-01
    • 2019-08-07
    • 2015-08-23
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多