我正在使用Rhino Mocks测试以下方法但遇到NullReferenceException:
public static List<string> RetrieveColumnNames(IDataReader reader)
{
List<string> columns = null;
for (int i = 0; i < reader.FieldCount; i++)
{
columns.Add(reader.GetName(i)); //Exception happened here when calling GetName
}
return columns;
}
[TestMethod()]
public void RetrieveColumnNamesTest()
{
//Arrange
IDataReader reader = MockRepository.GenerateStub<IDataReader>();
reader.Stub(x => x.FieldCount).Return(2);
reader.Stub(x => x.GetName(0)).IgnoreArguments().Return("First Name");
reader.Stub(x => x.GetName(1)).IgnoreArguments().Return("Second Name");
//Act
using (reader)
{
List<string> list = RetrieveColumnNames(reader);
//Assert
Assert.AreEqual(2, list.Count);
}
}
不太确定编排步骤有什么问题。