I am attempting to Mock an IDataRecord interface.
So far I have:
var mockIDataRecord = new Mock<IDataRecord>();
mockIDataRecord.SetupGet(c => c["id"]).Returns(7);
var z = mockIDataRecord["id"];
But Visual Studio throws a compilation error on the last line of that:
Error 2 Cannot apply indexing with [] to an expression of type 'Moq.Mock <System.Data.IDataRecord>'
Any suggestions?
The error is what visual studio says. You are applying indexing to instance of Mock
class, not its generic parameter ( IDataRecord
in your case). Use Mock.Object
Property that will return IDataRecord and apply indexing to it
var z = mockIDataRecord.Object["id"];
You have created a mock of an object (of type IDataRecord). However you are trying to access mockIDataRecord[id] which implies that mockIDataRecord is a collection (Array?).
The type mismatch is probably what is causing the error.
Can you try something like this instead (I haven't checked the syntax):
var mockIDataRecord = new Mock<IDataRecord[]>();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.