12

I have a bunch of classes that all implement an Interface and one of the parameters is a StreamWriter.

I need to check the contents of the StreamWriter.

I am trying to find a way to avoid writing text files on the test server and opening them to check the contents.

Is there is a way to quickly convert the StreamWriter contents/stream to a StringBuilder variable?

Damon
  • 1,249
  • 1
  • 15
  • 27
Yogurt The Wise
  • 4,379
  • 4
  • 34
  • 42

3 Answers3

22

You cannot check the StreamWriter. You could check the underlying stream it is writing to. So you could use a MemoryStream in your unit test and point this StreamWriter to it. Once it has finished writing you could read from it.

[TestMethod]
public void SomeMethod_Should_Write_Some_Expected_Output()
{
    // arrange
    using (var stream = new MemoryStream())
    using (var writer = new StreamWriter(stream))
    {
        // act
        sut.SomeMethod(writer);

        // assert
        string actual = Encoding.UTF8.GetString(stream.ToArray());
        Assert.AreEqual("some expected output", actual);
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
13

I would suggest you change the parameter to TextWriter if at all possible - at which point you can use a StringWriter.

Alternatively, you could create a StreamWriter around a MemoryStream, then test the contents of that MemoryStream later (either by rewinding it, or just calling ToArray() to get the complete contents as a byte array. If you really want to be testing text though, it's definitely simpler to use a StringWriter.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Maybe able to change it, but the interface is being used by several other classes. – Yogurt The Wise Sep 18 '12 at 16:06
  • 4
    @user295734: Anything which is *using* it will still be able to pass in a `StreamWriter` - but if you really just want something you can write text to, `TextWriter` is almost certainly a better abstraction to use. You should only need to change *implementations* of the interface... and I'd expect them to be fine. – Jon Skeet Sep 18 '12 at 16:24
  • If i was starting fresh, i would probably go back make the changes, there's just to much to change of other peoples code and such, that i don't have time to test all their code(guess they should have written unit tests for their code.) Thanks for the heads-up for next time. – Yogurt The Wise Sep 18 '12 at 19:49
0

In that case you need to mock the test case. You can use frameworks likes rhino mocks. That advantage of mocking framework is, you can verify the contents of the objects, but you don't have to hit server or occupy server resources.

This link will provide you the basic examples: http://www.codeproject.com/Articles/10719/Introducing-Rhino-Mocks

Priyank Thakkar
  • 4,752
  • 19
  • 57
  • 93
  • 2
    Why do you think mocking is required in this case? Mocking would help if the OP wanted to test how his code behaved in error cases, but to just find out what's being written, there's no need to use mocking. – Jon Skeet Sep 18 '12 at 15:57