Created
April 30, 2018 16:10
-
-
Save rubberduck203/c9e61bd6a6d22ae96e6d515f54e7b4f6 to your computer and use it in GitHub Desktop.
Example of unit testing .Net console input/output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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