Last active
January 20, 2025 16:29
-
-
Save asierba/ad9978c8b548f3fcef40 to your computer and use it in GitHub Desktop.
How to mock console in unit tests
This file contains 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
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
Console.WriteLine("What's your name?"); | |
var name = Console.ReadLine(); | |
Console.WriteLine(string.Format("Hello {0}!!", name)); | |
} | |
[Test] | |
public void something() | |
{ | |
var output = new StringWriter(); | |
Console.SetOut(output); | |
var input = new StringReader("Somebody"); | |
Console.SetIn(input); | |
Program.Main(new string[] { }); | |
Assert.That(output.ToString(), Is.EqualTo(string.Format("What's your name?{0}Hello Somebody!!{0}", Environment.NewLine))); | |
} | |
} |
👍
Thanks , good example.
many many thanks
how can take multi input??
if i have a one method and take multi console.readline().
please ans
how can take multi input??
if i have a one method and take multi console.readline().please ans
Every input will be part of the Console Input separated with a new line.
I've created another gist with multiple inputs to see this clearly: https://gist.github.com/asierba/3f51a7b82011bd171299fae307580cd8
How do we read numbers?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!