tags:

views:

319

answers:

4

Hello,

I use a stream reader to import some data and at the moment I hardcode a small sample file in the test to do the job.

Is it sensible to use Mock Objects with this and how ?

A: 

When testing code that depends on streams, streamreaders and streamwriters I usually use the memorystream object for testing. No mocking framework needed here.

Mendelt
+1  A: 

I don't see any points to mock StreamReader unless you're making StreamReader derived class. If you need to provide test input via StreamReader, just read some predefined data from any suitable source.

aku
+2  A: 

StreamReader is a concrete class, so many mocking systems won't allow you to mock it. TypeMock Isolator will, however.

You may find you want to mock it if you need to force errors to come from the reader, rather than just having it supply data to your class under test. If you don't need this functionality, you may be just as far ahead constructing a StreamReader from some other Stream, such as a MemoryStream - this way you don't need to go to disk for your data.

Blair Conrad
A: 

You can use a factory method to return a TextReader that could either be the mock object or an actual StreamReader.

Mark Cidade