Object Proxy


Consider this simplistic example, which uses a stream reader.

public class FileLoader
{
    private readonly StreamReader _streamReader;

    public FileLoader(StreamReader streamReader)
    {
        _streamReader = streamReader;
    }

    public IEnumerable<string> Read()
    {
        var output = new List<string>();
        string line;
        while (!string.IsNullOrEmpty(line = _streamReader.ReadLine()))
        {
            output.Add(line);
        }
        _streamReader.Close();
        _streamReader.Dispose();
        return output;
    }
}

The problem I have with this class is that it isn’t exactly unit testable. First of all, it uses a stream reader and it’s not straightforward to be able to mock a stream reader due to the lack of an interface. Secondly what I reckon most developers would do is pass a real stream reader into the class and write tests in that fashion, which would be more of an integration test.

Read the rest of this entry »

Posted in EdLib. Tags: . Leave a Comment »