本文介绍了C#单元测试一个方法调用Console.ReadLine()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为一个名为 ScoreBoard 的类的成员函数创建一个单元测试,该类存储一个游戏中的前五名玩家。

$ b $问题是,我创建了一个测试( SignInScoreBoard )的方法是调用 Console.ReadLine(),所以用户可以输入他们的名字:

  public void SignInScoreBoard(int steps)
{
if(topScored.Count< 5)
{
Console.Write(ASK_FOR_NAME_MESSAGE);
string name = Console.ReadLine();
KeyValuePair< string,int> pair = new KeyValuePair< string,int>(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< string,int>(name,steps));
}
}
}

像十个用户,所以我可以检查是否五个用较少的移动(步骤)被存储?

解决方案

您需要将调用Console.ReadLine的代码行重构为单独的对象,



作为一个简单的例子,你可以像这样创建一个类:

  public class ConsoleNameRetriever {
public virtual string GetNextName()
{
return Console.ReadLine();
}
}

然后,在您的方法中,重构它该类的实例。但是,在测试时,你可以用测试实现覆盖这个:

  public class TestNameRetriever:ConsoleNameRetriever {
/ /这应该给你的想法...
private string [] names = new string [] {Foo,Foo2,...};
private int index = 0;
public override string GetNextName()
{
return names [index ++];
}
}

测试时,实现。



授予,我个人使用一个框架使这更容易,并使用干净的接口,而不是这些实现,但希望上面是足够给你正确的想法...


I want to create a unit test for a member function of a class called ScoreBoard which is storing the top five players in a game.

The problem is that the method I created a test for (SignInScoreBoard) is calling Console.ReadLine() so the user can type their name:

public void SignInScoreBoard(int steps)
{
    if (topScored.Count < 5)
    {
        Console.Write(ASK_FOR_NAME_MESSAGE);
        string name = Console.ReadLine();
        KeyValuePair<string, int> pair = new KeyValuePair<string, int>(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<string, int>(name, steps));
        }
    }
}

Is there a way to insert like ten users so I can check if the five with less moves (steps) are being stored?

解决方案

You'll need to refactor the lines of code that call Console.ReadLine into a separate object, so you can stub it out with your own implementation in your tests.

As a quick example, you could just make a class like so:

public class ConsoleNameRetriever {
     public virtual string GetNextName()
     {
         return Console.ReadLine();
     }
}

Then, in your method, refactor it to take an instance of this class instead. However, at test time, you could override this with a test implementation:

public class TestNameRetriever : ConsoleNameRetriever {
     // This should give you the idea...
     private string[] names = new string[] { "Foo", "Foo2", ... };
     private int index = 0;
     public override string GetNextName()
     {
         return names[index++];
     }
}

When you test, swap out the implementation with a test implementation.

Granted, I'd personally use a framework to make this easier, and use a clean interface instead of these implementations, but hopefully the above is enough to give you the right idea...

这篇关于C#单元测试一个方法调用Console.ReadLine()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:33