Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将单元测试应用于需要用户动态输入的C#函数?_C#_Unit Testing_Input_Tdd - Fatal编程技术网

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

如何将单元测试应用于需要用户动态输入的C#函数?,c#,unit-testing,input,tdd,C#,Unit Testing,Input,Tdd,下面的函数从用户处获取输入。我需要使用单元测试来测试这个函数。有人能告诉我如何测试这种需要用户动态输入的功能吗。谢谢 像边值分析 numberOfCommands应该是(0创建一个接口并传入该接口以接收文本。然后,在单元测试中,传入一个自动返回某些结果的模拟接口 编辑代码详细信息: public interface IUserInput{ string GetInput(); } public static int Get_Commands(IUserInput input){

下面的函数从用户处获取输入。我需要使用单元测试来测试这个函数。有人能告诉我如何测试这种需要用户动态输入的功能吗。谢谢

像边值分析


numberOfCommands
应该是
(0创建一个接口并传入该接口以接收文本。然后,在单元测试中,传入一个自动返回某些结果的模拟接口

编辑代码详细信息:

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());
 }

您可以将文件中的输入重定向到标准输入,并在测试中使用。您可以在程序本身中以编程方式或通过运行程序的shell来执行此操作

您还可以将所有“用户输入”外推到它们自己的类/函数中,这样就可以很容易地将“从用户处获取一行文本”的函数替换为“返回此硬编码字符串以进行测试”的函数。如果这些函数中的每一个都在实现公共接口的类中,则可以很容易地将它们切换出去.

在Main()中,您可以执行以下操作:

int testCommand=Get_Commands();
Console.WriteLine(testCommand);
但是,我不知道这是否是您想要的测试类型。问题是否不仅仅是测试函数的结果?

两件基本的事情:

  • Console.ReadLine
    是一个外部依赖项,应该以某种方式提供给您的方法(最好通过依赖项注入)
  • Console.ReadLine
    在引擎盖下使用基类,这就是应该提供的
  • 因此,您的方法需要的是对
    TextReader
    的依赖性(您可以使用自定义接口对其进行更多抽象,但为了测试它就足够了):

    现在,在实际应用程序中,您可以使用real console调用
    Get_命令

        int commandsNumber = Get_Commands(Console.In);
    
    在单元测试中,您使用例如
    StringReader
    类创建假输入:

    [Test]
    public void Get_Commands_ReturnsCorrectNumberOfCommands()
    {
       const string InputString = 
           "150" + Environment.NewLine + 
           "10" + Environment.NewLine;
       var stringReader = new StringReader(InputString);
    
       var actualCommandsNumber = MyClass.Get_Commands(stringReader);
    
       Assert.That(actualCommandsNumber, Is.EqualTo(10));
    }
    

    您可以使用
    Console.SetIn()
    Console.SetOut()
    定义输入和输出。使用StringReader定义测试的输入,使用StringWriter捕获输出


    你可以看到我关于这个主题的博客文章,以获得更完整的解释和示例:

    只需运行程序。给出输入。你的意思是单元测试吗?对单元测试来说是的。
    numberOfCommands
    的边界值测试应该是
    0+1,也称为“依赖项注入”。这是正确的方法。
    
        int commandsNumber = Get_Commands(Console.In);
    
    [Test]
    public void Get_Commands_ReturnsCorrectNumberOfCommands()
    {
       const string InputString = 
           "150" + Environment.NewLine + 
           "10" + Environment.NewLine;
       var stringReader = new StringReader(InputString);
    
       var actualCommandsNumber = MyClass.Get_Commands(stringReader);
    
       Assert.That(actualCommandsNumber, Is.EqualTo(10));
    }