gpt4 book ai didi

c# - 模拟 DataReader 并获取 Rhino.Mocks.Exceptions.ExpectationViolationException : IDisposable. Dispose();预期#0,实际#1

转载 作者:可可西里 更新时间:2023-11-01 08:45:21 25 4
gpt4 key购买 nike

我正在尝试模拟一个 SqlDataReader

 SqlDataReader reader = mocks.CreateMock<SqlDataReader>();
Expect.Call(reader.Read()).Return(true).Repeat.Times(1);
Expect.Call(reader.Read()).Return(false);
Expect.Call(reader.HasRows).Return(true);
Expect.Call(reader.Dispose);
Expect.Call(reader["City"]).Return("Boise");
Expect.Call(reader["State"]).Return("State");
Expect.Call(reader["LAT"]).Return(100);
Expect.Call(reader["LON"]).Return(-100);
mocks.ReplayAll();

但我不断收到 Rhino.Mocks.Exceptions.ExpectationViolationException: IDisposable.Dispose();我的方法中预期错误 #0,实际错误 #1

        using (reader)
{
if (reader.HasRows)
{
while (reader.Read())
{
CityState myCity = new CityState
{
City = reader["City"].ToString(),
State = reader["State"].ToString(),
Lat = Convert.ToInt32(reader["LAT"]),
Lon = Convert.ToInt32(reader["LON"])
};
myCities.Add(myCity);
}
}
}

我错过了什么吗?

最佳答案

我建议您使用 AAA 语法重写测试并使用接口(interface) (IDataReader) 而不是具体类型 (SqlDataReader):

// Arrange
IDataReader reader = MockRepository.GenerateStub<IDataReader>();
reader.Stub(x => x.Read()).Repeat.Once().Return(true);
reader.Stub(x => x.Read()).Return(false);
reader.Stub(x => x["City"]).Return("Boise");
reader.Stub(x => x["State"]).Return("State");
reader.Stub(x => x["LAT"]).Return(100);
reader.Stub(x => x["LON"]).Return(-100);

// Act
var myCities = new List<CityState>();
using (reader)
{
while (reader.Read())
{
CityState myCity = new CityState
{
City = reader["City"].ToString(),
State = reader["State"].ToString(),
Lat = Convert.ToInt32(reader["LAT"]),
Lon = Convert.ToInt32(reader["LON"])
};
myCities.Add(myCity);
}
}

// Assert
Assert.AreEqual(1, myCities.Count);
Assert.AreEqual("Boise", myCities[0].City);
...

关于c# - 模拟 DataReader 并获取 Rhino.Mocks.Exceptions.ExpectationViolationException : IDisposable. Dispose();预期#0,实际#1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1792984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com