模拟IDBEngine以使用Moq返回模拟的DbDataReader

时间:2019-07-26 10:57:43

标签: c# .net moq ado

以下测试:

Mock<IDBEngine> dbEngineMock = new Mock<IDBEngine>();
Mock<DbDataReader> dbReader = new Mock<DbDataReader>();

dbEngineMock.Setup(x => x.Dispose());
dbEngineMock.Setup(x => x.ExecuteReader(It.IsAny<string>())).Returns(dbReader.Object);

dbReader.SetupSequence(x => x.Read()).Returns(true).Returns(false);
dbReader.Setup(x => x["Column_Name"]).Returns(importFlag);

SomeClass myInstance = new SomeClass();
Assert.NotNull(myInstance.DoStuff(dbEngineMock.Object))

每次dbEngineMock必须执行ExecuteReader()方法时,返回 null

private int? DoStuff(IDbEngine aDBEngine)
{
    using (DbDataReader reader = aDBEngine.ExecuteReader(select))
    {
       while (reader.Read()) //null reference exception
       {

我想念什么吗?

有关DbDataReader的更多信息:

public abstract class DbDataReader : MarshalByRefObject, IDataReader, IDisposable, IDataRecord, IEnumerable

和IDBEngine:

 public interface IDBEngine : IDisposable
    {
    DbDataReader ExecuteReader(string sqlCommand);

0 个答案:

没有答案