views:

93

answers:

2

I have a method in my business logic layer that accepts a stream, which in the GUI comes from a user uploading a file, and I am interested in which is an appropriate way to test that the method appropriately uses this stream to make decisions.

public Sub Initialize(ByVal uploadStream As Stream)
    ''// Logic using uploadStream
End Sub

For testing purposes I wish to DI a mocked stream into this method, but I find a stiffling lack of abstraction whenever working with streams.

Intuition tells me that a need to create a Stream wrapper which would allow me to DI an interface of the wrapper to test interaction of my logic with the stream wapper.

What's the best way to proceed?

+3  A: 

If you just want a way to pass in a "fake" upload, you could construct a MemoryStream in your test harness and pass that in.

John Price
+1  A: 

I too tend to use a MemoryStream. For some tests you might want to overload the Read method to return less than the requested number of bytes. (I think a MemoryStream will always return the requested number of bytes, unless it'a reached the end of the stream, but a network stream can return less bytes than requested even before the end of the stream.)

it depends