Here’s the scenario:
I have a method that reads in a file via a FileStream and a StreamReader in .NET. I would like to unit test this method and somehow remove the dependency on the StreamReader object.
Ideally I would like to be able to supply my own string of test data instead of using a real file. Right now the method makes use of the StreamReader.ReadLine method throughout. What is an approach to modifying the design I have now in order to make this test possible?
Depend on
StreamandTextReaderinstead. Then your unit tests can useMemoryStreamandStringReader. (Or load resources from inside your test assembly if necessary.)Note how
ReadLineis originally declared byTextReader, notStreamReader.