模拟迭代行为

时间:2011-06-22 10:58:54

标签: c# unit-testing tdd mocking rhino-mocks

我有一个迭代行为的界面,我在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);


    }

3 个答案:

答案 0 :(得分:4)

这就是你所需要的:

var enumerator = new List<string> { "A", "B", "C" }.GetEnumerator();
var lineReader = MockRepository.GenerateStub<ILineReader>();

lineReader.Stub(x => x.CurrentLine())
    .Return("ignored")
    .WhenCalled(x => x.ReturnValue = enumerator.Current);

lineReader.Stub(x => x.ReadLine())
    .Return(false) // will be ignored
    .WhenCalled(x => x.ReturnValue = enumerator.MoveNext());

答案 1 :(得分:2)

这似乎可以解决问题:

[TestFixture]
public sealed class TestIterativeRhinoReturn
{
    private int _count;
    private int _countOfLines; 
    private IList<string> _lines;
    private string _currentLine; 
    [SetUp]
    public void SetUp()
    {
        _count = -1;

        _lines= new List<string>()
                                            {
                                                "A",
                                                "B",
                                                "C",
                                                null
                                            };


        _countOfLines = _lines.Count;
        _currentLine = null; 
    }


    [Test]
    public void Test()
    {

        MockRepository mockRepository = new MockRepository();
        ILineReader lineReader = mockRepository.DynamicMock<ILineReader>();

        lineReader.Stub(r => r.ReadLine()).Callback(new ReadLineDelegate(ReadRecord)).Return(_count < _countOfLines);
        lineReader.Stub(r => r.CurrentLine()).Do(new CurrentStringDelegate(ReturnString)).Return(_currentLine);

        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 read4 = lineReader.ReadLine();
        Assert.That(read4, Is.False);
        Assert.That(lineReader.CurrentLine(), Is.Null);


    }


    public delegate bool ReadLineDelegate();

    private bool ReadRecord()
    {
        _count++;
        return (_lines[_count]!=null);
    }

    public delegate string CurrentStringDelegate(); 

    private string ReturnString()
    {
        return _lines[_count]; 
    }

注意线条:

        lineReader.Stub(r => r.ReadLine()).Callback(new ReadLineDelegate(ReadRecord)).Return(_count < _countOfLines);
        lineReader.Stub(r => r.CurrentLine()).Do(new CurrentStringDelegate(ReturnString)).Return(_currentLine);

和Delegate方法ReadRecord()和ReturnString()。现在,每次读取的返回值都会发生变化。

答案 2 :(得分:1)

我不知道我是否拥有最新版本的rhino-mocks但我的.Return方法没有采取委托/行动 - 这可能对你想做的事情有用。

但是,你似乎更接近于基于状态的测试,所以可能只是创建自己的模拟实现进行测试(或者存根,或假 - 你选择:)

然后你可以控制你所追求的确切值。