Tag: console redirect

调用Console.ReadLine()的方法的C#unit testing

我想为一个名为ScoreBoard的类的成员函数创建一个unit testing,该类存储游戏中的前五名玩家。 问题是我为( SignInScoreBoard )创建测试的方法是调用Console.ReadLine()因此用户可以输入他们的名字: public void SignInScoreBoard(int steps) { if (topScored.Count < 5) { Console.Write(ASK_FOR_NAME_MESSAGE); string name = Console.ReadLine(); KeyValuePair pair = new KeyValuePair(name, steps); topScored.Insert(topScored.Count, pair); } else { if (steps < topScored[4].Value) { topScored.RemoveAt(4); Console.Write(ASK_FOR_NAME_MESSAGE); string name = Console.ReadLine(); topScored.Insert(4, new KeyValuePair(name, steps)); } } } 有没有办法插入十个用户,所以我可以检查是否存储了五个较少的移动(步骤)?

将控制台输出重定向到winforms ListBox

我已经构建了一个库,使用Console.WriteLine()转储其大部分调试文本; 我现在是在Windows窗体应用程序中使用库的过程,仍然需要访问控制台输出。 (要在List / RichText框中显示) 我注意到我可以将控制台的标准覆盖到TextWriter,但是如何将这些数据放入显示器中。 我试着做一些事情 public partial class Form1 : Form { Timer T; MemoryStream mem; StreamWriter writer; public Form1() { InitializeComponent(); mem = new MemoryStream(1000); writer = new StreamWriter(mem); Console.SetOut(writer); T = new Timer(); T.Interval = 250; // yes this probally is to short. T.Tick += new EventHandler(T_Tick); T.Start(); Console.WriteLine(“output”); Console.WriteLine(“AnotherLine”); } […]