How to mock an SqlDataReader using Moq - Update

后端 未结 5 1430
温柔的废话
温柔的废话 2021-01-30 17:12

I\'m new to moq and setting up mocks so i could do with a little help. How do I mock up an SqlDataReader using Moq?

Update

After further testing this is what I h

5条回答
  •  死守一世寂寞
    2021-01-30 17:28

    I was just trying to figure this out myself. Not sure if this is new functionality in Moq, but it appears there is a simpler way than @Monsignor's answer.

    Use Moq's SetupSequence method. Your code simply becomes:

    private IDataReader MockIDataReader()
    {
        var moq = new Mock();
        moq.SetupSequence( x => x.Read() )
           .Returns( true )
           .Returns( false );
        moq.SetupGet( x => x["Char"] ).Returns( 'C' );
    
        return moq.Object; 
    }
    
        

    提交回复
    热议问题