Does that seem ok to you or is there a better way? I plan to use this everywhere in my code that needs to be tested. I'm using moq framework and SystemWrapper for other mscorlib methods I need to stub/mock.

That way you get rid of the hard-coded default StreamWriter. But there are a number of issues with this approach.

TextWriter being an abstract class is nice, but this abstract class implements the IDisposable interface, which makes it a leaky abstraction to use as a dependency.

The class is still tightly coupled with StringWriter, through the new StringWriter() call in the default constructor. This is actually a DI anti-pattern that Mark Seemann calls bastard injection in Dependency Injection in .NET.

You're dealing with an IDisposable here; I'd expect this test code to throw a rather surprising ObjectDisposedException:

You need an abstraction to wrap it with, and be able to inject an implementation that doesn't hit the file system, so you can test your code; the simplest abstraction remains an interface - it doesn't tie you up with a base class with embedded behavior that you don't want your tests to have to care for.. or worry about.

I would simply wrap the writer with an interface, with the bare minimum members you need for it to work with your code:

Now you can constructor-inject an IWriter instead of a "factory", and your tests can inject a mock implementation, and the mock can know if the Write method was called with the expected value parameter.

Note that I could have done this too:

public interface IWriter : IDisposable
{
void Write(string value);
}

But that would have been a leaky abstraction - the fact that we're encapsulating a TextWriter that implements IDisposable shouldn't be leaking into the abstraction, because the TextWriter isn't the abstraction, it's an implementation detail: you could just as well write an implementation that doesn't use a StreamWriter and isn't disposable - for example:

\$\begingroup\$Thank you, I followed your idea and passed the parameters through the Write() method. This also greatly simplified the Writer class, as no constructor or Dispose() are needed anymore (all the magic happens inside Write() method).\$\endgroup\$
– ThunderDevAug 27 '14 at 14:50

\$\begingroup\$Great! Feel free to mark as accepted, and come back anytime you have more code ready to be peer reviewed! :)\$\endgroup\$
– Mathieu GuindonAug 27 '14 at 14:56