I have an interface with iterative behavior, and I am having problems with Mocking, which is in Rhinomocks. An example of an interface and class is a very simple version of my problem.
Each time LineReader.Read () is called, LineReader.CurrentLine () should return another value - the next line. I still could not reproduce this behavior. Thus, he became my small hobby project, to which I come back from time to time. I hope you help me even further.
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);
}
}
EDIT Incomplete unit test added :
[Test]
public void Test()
{
IList<string> lineListForMock = new List<string>()
{
"A",
"B",
"C"
};
MockRepository mockRepository = new MockRepository();
ILineReader lineReader = mockRepository.Stub<ILineReader>();
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);
}
source
share