我有一個迭代行爲的接口,並且我在Rhinomocks中嘲笑它。示例界面和類是我的問題的一個非常簡單的版本。嘲諷迭代行爲
每次調用LineReader.Read()時,LineReader.CurrentLine()都會返回一個不同的值 - 下一行。這種行爲我至今還沒有能夠在模擬中重現。因此,它已成爲我不時迴歸的一個小型業餘愛好項目。我希望你能進一步幫助我。
internal class LineReader : ILineReader
{
private readonly IList<string> _lines;
private int _countOfLines;
private int _place;
public LineReader(IList<string> lines)
{
_lines = lines;
_countOfLines = lines.Count;
_place = 0;
}
public string CurrentLine()
{
if (_place<_countOfLines)
{
return _lines[_place];
}
else
{
return null;
}
}
public bool ReadLine()
{
_place++;
return (_place < _countOfLines);
}
}
編輯不完整的單元測試添加:
[Test]
public void Test()
{
IList<string> lineListForMock = new List<string>()
{
"A",
"B",
"C"
};
MockRepository mockRepository = new MockRepository();
ILineReader lineReader = mockRepository.Stub<ILineReader>();
//Setup the values here
mockRepository.ReplayAll();
bool read1 = lineReader.ReadLine();
Assert.That(read1, Is.True);
Assert.That(lineReader.CurrentLine(), Is.EqualTo("A"));
bool read2 = lineReader.ReadLine();
Assert.That(read2, Is.True);
Assert.That(lineReader.CurrentLine(), Is.EqualTo("B"));
bool read3 = lineReader.ReadLine();
Assert.That(read3, Is.True);
Assert.That(lineReader.CurrentLine(), Is.EqualTo("C"));
bool read1 = lineReader.ReadLine();
Assert.That(read1, Is.False);
}
什麼時候開始一些代碼爲你的單元測試? –
@Thomas,給我兩分鐘:o) – Morten