I liked the idea of JaredPar , but I did not want to go through Console.Out and Console.Error for every auxiliary output method that I had. However, my output goes through one class, so I just set a couple of static fields in it:
internal static TextWriter _stdOut = Console.Out; internal static TextWriter _stdErr = Console.Error;
I updated my output methods in the output handler class to use these fields. Then I updated this AssemblyInfo.cs project to include:
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("MyTestProject")]
That way, I can override _stdOut and _stdErr in my testing methods, call my test method (which uses my output processing class) and confirm the expected result.
OutputHandler._stdOut = new StringWriter(); MySnazzyMethod("input", 1, 'c'); OutputHandler._stdOut.Flush(); string expected = "expected output"; string stdout = OutputHandler._stdOut.ToString().Trim(new[] { '\r', '\n' }); Assert.IsFalse(string.IsNullOrEmpty(stdout)); Assert.AreEqual(expected, stdout);
Sarah vessels
source share