以下测试:
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);