【发布时间】:2011-06-22 10:58:54
【问题描述】:
我有一个带有迭代行为的接口,但在 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)
标签: c# unit-testing tdd mocking rhino-mocks