如何将单元测试应用于需要动态用户输入的C#功能?

ran*_*ana 7 c# tdd unit-testing input

以下功能从用户获得输入.我需要使用测试这个功能Unit Testing.任何人都可以告诉我如何测试这种动态需要用户输入的功能.谢谢

喜欢boundary value analysis......

numberOfCommands 应该 (0 <= n <= 100)

public static int Get_Commands()
{
    do
    {
        string noOfCommands = Console.ReadLine().Trim();
        numberOfCommands = int.Parse(noOfCommands);             
    }
    while (numberOfCommands <= 0 || numberOfCommands >= 100);  

    return numberOfCommands;
}
Run Code Online (Sandbox Code Playgroud)

以编程方式提示将是非常有帮助的!

Jos*_*osh 11

创建一个接口并传入接口以接收文本.然后,在您的单元测试中,传入一个自动返回某些结果的模拟界面.

编辑代码详细信息:

public interface IUserInput{
    string GetInput();
}

public static int Get_Commands(IUserInput input){
    do{
       string noOfCommands = input.GetInput();
       // Rest of code here
    }
 }

public class Something : IUserInput{
     public string GetInput(){
           return Console.ReadLine().Trim();
     }
 }

 // Unit Test
 private class FakeUserInput : IUserInput{
      public string GetInput(){
           return "ABC_123";
      }
 }
 public void TestThisCode(){
    GetCommands(new FakeUserInput());
 }
Run Code Online (Sandbox Code Playgroud)