Skip to content

Instantly share code, notes, and snippets.

@rubberduck203
Created April 30, 2018 16:10
Show Gist options
  • Save rubberduck203/c9e61bd6a6d22ae96e6d515f54e7b4f6 to your computer and use it in GitHub Desktop.
Save rubberduck203/c9e61bd6a6d22ae96e6d515f54e7b4f6 to your computer and use it in GitHub Desktop.
Example of unit testing .Net console input/output
[Fact]
public void ExampleForTakingControlOfConsole()
{
var stream = new System.IO.MemoryStream();
using (var writer = new System.IO.StreamWriter(stream))
using (var reader = new System.IO.StreamReader(stream))
{
writer.WriteLine("quit");
writer.Flush();
stream.Position = 0; //reset stream before reading
Console.SetIn(reader);
Assert.Equal("quit", Console.ReadLine());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment